1- import { encodeQuery , parseQuery } from '../utilities'
1+ import { encodeQuery } from '../utilities'
22import nanoid from 'nanoid'
3+ const isHttps = process . server ? require ( 'is-https' ) : null
34
45const DEFAULTS = {
56 token_type : 'Bearer' ,
@@ -10,6 +11,7 @@ const DEFAULTS = {
1011export default class Oauth2Scheme {
1112 constructor ( auth , options ) {
1213 this . $auth = auth
14+ this . req = auth . ctx . req
1315 this . name = options . _name
1416
1517 this . options = Object . assign ( { } , DEFAULTS , options )
@@ -28,6 +30,12 @@ export default class Oauth2Scheme {
2830 return url
2931 }
3032
33+ if ( process . server && this . req ) {
34+ const protocol = 'http' + ( isHttps ( this . req ) ? 's' : '' ) + '://'
35+
36+ return protocol + this . req . headers . host + this . $auth . options . redirect . callback
37+ }
38+
3139 if ( process . client ) {
3240 return window . location . origin + this . $auth . options . redirect . callback
3341 }
@@ -91,7 +99,7 @@ export default class Oauth2Scheme {
9199 opts . nonce = nonce || nanoid ( )
92100 }
93101
94- this . $auth . $storage . setLocalStorage ( this . name + '.state' , opts . state )
102+ this . $auth . $storage . setUniversal ( this . name + '.state' , opts . state )
95103
96104 const url = this . options . authorization_endpoint + '?' + encodeQuery ( opts )
97105
@@ -116,28 +124,34 @@ export default class Oauth2Scheme {
116124 }
117125
118126 async _handleCallback ( uri ) {
119- // Callback flow is not supported in server side
120- if ( process . server ) {
127+ // Handle callback only for specified route
128+ if ( this . $auth . options . redirect && this . $auth . ctx . route . path !== this . $auth . options . redirect . callback ) {
129+ return
130+ }
131+ // Callback flow is not supported in static generation
132+ if ( process . server && process . static ) {
121133 return
122134 }
123135
124- // Parse query from both search and hash fragments
125- const hash = parseQuery ( window . location . hash . substr ( 1 ) )
126- const search = parseQuery ( window . location . search . substr ( 1 ) )
127- const parsedQuery = Object . assign ( { } , search , hash )
128-
136+ const parsedQuery = Object . assign ( { } , this . $auth . ctx . route . query , this . $auth . ctx . route . hash )
129137 // accessToken/idToken
130138 let token = parsedQuery [ this . options . token_key || 'access_token' ]
131-
132139 // refresh token
133140 let refreshToken = parsedQuery [ this . options . refresh_token_key || 'refresh_token' ]
134141
142+ // Validate state
143+ const state = this . $auth . $storage . getUniversal ( this . name + '.state' )
144+ this . $auth . $storage . setUniversal ( this . name + '.state' , null )
145+ if ( state && parsedQuery . state !== state ) {
146+ return
147+ }
148+
135149 // -- Authorization Code Grant --
136150 if ( this . options . response_type === 'code' && parsedQuery . code ) {
137- const data = await this . $auth . request ( {
151+ let data = await this . $auth . request ( {
138152 method : 'post' ,
139153 url : this . options . access_token_endpoint ,
140- baseURL : false ,
154+ baseURL : process . server ? undefined : false ,
141155 data : encodeQuery ( {
142156 code : parsedQuery . code ,
143157 client_id : this . options . client_id ,
@@ -161,13 +175,6 @@ export default class Oauth2Scheme {
161175 return
162176 }
163177
164- // Validate state
165- const state = this . $auth . $storage . getLocalStorage ( this . name + '.state' )
166- this . $auth . $storage . setLocalStorage ( this . name + '.state' , null )
167- if ( state && parsedQuery . state !== state ) {
168- return
169- }
170-
171178 // Append token_type
172179 if ( this . options . token_type ) {
173180 token = this . options . token_type + ' ' + token
0 commit comments