@@ -5,43 +5,45 @@ import { NextRequest } from 'next/server'
55const errRedir = ( err : any ) => redirect ( '/slack-error?err=' + err . toString ( ) )
66
77export async function GET ( request : NextRequest ) {
8- return redirect ( '/slack-error?err=High Seas has ended! Sign-ups are disabled.' )
9- /*
8+ // Try to authenticate the user first
109 const code = request . nextUrl . searchParams . get ( 'code' )
11-
12- const redirectUri = await getRedirectUri()
13-
14- const exchangeUrl = `https://slack.com/api/openid.connect.token?code=${code}&client_id=${process.env.SLACK_CLIENT_ID}&client_secret=${process.env.SLACK_CLIENT_SECRET}&redirect_uri=${redirectUri}`
15- console.log('exchanging by posting to', exchangeUrl)
16-
17- const res = await fetch(exchangeUrl, { method: 'POST' })
18-
19- if (res.status !== 200) return errRedir('Bad Slack OpenId response status')
20-
21- let data
22- try {
23- data = await res.json()
24- } catch (e) {
25- console.error(e, await res.text())
26- throw e
27- }
28- if (!data || !data.ok) {
29- console.error(data)
30- return errRedir('Bad Slack OpenID response')
10+ if ( ! code ) {
11+ return redirect ( '/slack-error?err=Missing authorization code' )
3112 }
3213
3314 try {
34- await createSlackSession(data.id_token)
35- console.log('cretaed slack session!! :)))))')
36- } catch (e) {
37- return errRedir(e)
15+ const redirectUri = await getRedirectUri ( )
16+
17+ const exchangeUrl = `https://slack.com/api/openid.connect.token?code=${ code } &client_id=${ process . env . SLACK_CLIENT_ID } &client_secret=${ process . env . SLACK_CLIENT_SECRET } &redirect_uri=${ redirectUri } `
18+ console . log ( 'exchanging by posting to' , exchangeUrl )
19+
20+ const res = await fetch ( exchangeUrl , { method : 'POST' } )
21+
22+ if ( res . status !== 200 ) return errRedir ( 'Bad Slack OpenId response status' )
23+
24+ let data
25+ try {
26+ data = await res . json ( )
27+ } catch ( e ) {
28+ console . error ( e , await res . text ( ) )
29+ throw e
30+ }
31+ if ( ! data || ! data . ok ) {
32+ console . error ( data )
33+ return errRedir ( 'Bad Slack OpenID response' )
34+ }
35+
36+ try {
37+ await createSlackSession ( data . id_token )
38+ console . log ( 'created slack session!! :)))))' )
39+ return redirect ( '/signpost' )
40+ } catch ( e : any ) {
41+ if ( e . toString ( ) . includes ( 'Sign-ups are disabled' ) ) {
42+ return errRedir ( e )
43+ }
44+ throw e
45+ }
46+ } catch ( error ) {
47+ return errRedir ( error )
3848 }
39-
40- // const userInfoUrl = `https://slack.com/api/openid.connect.userInfo`;
41- // const userInfo = await fetch(userInfoUrl, {
42- // headers: { Authorization: `Bearer ${data.access_token}` },
43- // }).then((d) => d.json());
44-
45- redirect('/signpost')
46- */
4749}
0 commit comments