@@ -4,8 +4,6 @@ import { getRandomString, generateVerifier, computeChallenge } from 'src/utils'
44
55
66const client_id = process . env . CLIENT_ID
7- const authorityUrl = process . env . AUTHORITY_URL
8-
97
108export async function signIn ( ) {
119 const state = getRandomString ( 16 )
@@ -15,16 +13,22 @@ export async function signIn() {
1513 const codeChallenge = await computeChallenge ( codeVerifier )
1614
1715 const params = new URLSearchParams ( {
18- response_type : 'code' ,
1916 client_id : `${ client_id } ` ,
2017 redirect_uri : `${ window . location . origin } /callback` ,
18+ state : state ,
2119 scope : 'openid profile' ,
20+ response_type : 'code' ,
2221 code_challenge : codeChallenge ,
23- state : state ,
2422 code_challenge_method : 'S256'
2523 } )
2624
27- window . location . href = `${ authorityUrl } /${ SERVER_URL . AUTHORIZE } ?${ params } `
25+ api . get ( SERVER_URL . AUTHORIZE , { params } ) . then ( res => {
26+ window . location . replace ( res . request . responseURL )
27+ } ) . catch ( error => {
28+ if ( error ) {
29+ window . location . replace ( '/login' )
30+ }
31+ } )
2832}
2933
3034export function handleCallback ( ) {
@@ -46,28 +50,29 @@ export function handleCallback() {
4650 }
4751
4852 const params = new URLSearchParams ( {
49- grant_type : 'authorization_code' ,
5053 client_id : `${ client_id } ` ,
51- code : code ,
5254 redirect_uri : `${ window . location . origin } /callback` ,
55+ state : state ,
56+ code : code ,
5357 code_verifier : codeVerifier ,
54- state : state
58+ grant_type : 'authorization_code'
5559 } )
5660 // Exchange authorization code for access token
57- return api . post ( ` ${ authorityUrl } / ${ SERVER_URL . TOKEN } ` , params )
61+ return api . post ( SERVER_URL . TOKEN , params )
5862}
5963
6064export function getSub ( ) {
61- return api . get ( ` ${ authorityUrl } / ${ SERVER_URL . USERINFO } ` )
65+ return api . get ( SERVER_URL . USERINFO )
6266}
6367
64- export function signOut ( ) {
65- const idTokenHint = localStorage . getItem ( 'id_token_hint' )
68+ export function signOut ( idToken : string ) {
6669 const params = new URLSearchParams ( {
67- id_token_hint : ` ${ idTokenHint } ` ,
70+ id_token_hint : idToken ,
6871 client_id : `${ client_id } ` ,
6972 post_logout_redirect_uri : `${ window . location . origin } `
7073 } )
7174
72- window . location . href = `${ authorityUrl } /${ SERVER_URL . LOGOUT } ?${ params } `
75+ api . post ( SERVER_URL . LOGOUT , params ) . then ( res => {
76+ window . location . replace ( res . request . responseURL )
77+ } )
7378}
0 commit comments