@@ -5,45 +5,49 @@ import { NextRequest } from 'next/server'
55const errRedir = ( err : any ) => redirect ( '/slack-error?err=' + err . toString ( ) )
66
77export async function GET ( request : NextRequest ) {
8- // Try to authenticate the user first
98 const code = request . nextUrl . searchParams . get ( 'code' )
10- if ( ! code ) {
11- return redirect ( '/slack-error?err=Missing authorization code' )
9+
10+ const redirectUri = await getRedirectUri ( )
11+
12+ 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 } `
13+
14+ console . log ( 'exchanging by posting to' , exchangeUrl )
15+
16+ const res = await fetch ( exchangeUrl , { method : 'POST' } )
17+
18+ if ( res . status !== 200 ) return errRedir ( 'Bad Slack OpenId response status' )
19+
20+ let data
21+
22+ try {
23+ data = await res . json ( )
24+ } catch ( e ) {
25+ console . error ( e , await res . text ( ) )
26+
27+ throw e
28+ }
29+
30+ if ( ! data || ! data . ok ) {
31+ console . error ( data )
32+
33+ return errRedir ( 'Bad Slack OpenID response' )
1234 }
1335
1436 try {
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 )
37+ await createSlackSession ( data . id_token )
38+
39+ console . log ( 'cretaed slack session!! :)))))' )
40+ } catch ( e ) {
41+ return errRedir ( e )
4842 }
43+
44+ // const userInfoUrl = `https://slack.com/api/openid.connect.userInfo`;
45+
46+ // const userInfo = await fetch(userInfoUrl, {
47+
48+ // headers: { Authorization: `Bearer ${data.access_token}` },
49+
50+ // }).then((d) => d.json());
51+
52+ redirect ( '/signpost' )
4953}
0 commit comments