@@ -7,78 +7,76 @@ import { TokenParam, defaultParam } from './token';
7
7
* as dictated by netlify identity's communication with us via hashes
8
8
*/
9
9
10
- const routes = / ( c o n f i r m a t i o n | i n v i t e | r e c o v e r y | e m a i l _ c h a n g e ) _ t o k e n = ( [ ^ & ] + ) / ;
10
+ const routes = / ( c o n f i r m a t i o n | i n v i t e | r e c o v e r y | e m a i l _ c h a n g e | a c c e s s ) _ t o k e n = ( [ ^ & ] + ) / ;
11
11
const errorRoute = / e r r o r = a c c e s s _ d e n i e d & e r r o r _ d e s c r i p t i o n = 4 0 3 / ;
12
- const accessTokenRoute = / a c c e s s _ t o k e n = / ;
13
- const confirmationRoute = / c o n f i r m a t i o n _ t o k e n = / ;
12
+
13
+ const reduceHashToKeyValue = ( hash : string ) : { [ key : string ] : string } =>
14
+ hash . split ( '&' ) . reduce ( ( carry , pair ) => {
15
+ const [ key , value ] = pair . split ( '=' ) ;
16
+
17
+ return { ...carry , [ key ] : value } ;
18
+ } , { } ) ;
19
+
20
+ const hashReplace = / ^ # \/ ? / ;
14
21
15
22
export function runRoutes (
16
23
gotrue : GoTrue ,
17
24
setUser : ( value : User ) => User | undefined ,
18
25
remember = true
19
26
) : TokenParam {
20
27
// early terminate if no hash
28
+ // also accounts for document.cookie further down
21
29
if ( ! document ?. location ?. hash ) {
22
30
return defaultParam ;
23
31
}
24
32
25
- const hash = document . location . hash . replace ( / ^ # \/ ? / , '' ) ;
33
+ const hash = document . location . hash . replace ( hashReplace , '' ) ;
26
34
27
- const m = hash . match ( routes ) ;
28
- if ( m ) {
29
- // store.verifyToken(m[1], m[2]);
30
- document . location . hash = '' ;
31
- }
35
+ // todo: maybe replace with history.replaceState to completely clear the url?
36
+ // currently keeps #
37
+ document . location . hash = '' ;
32
38
33
- const em = hash . match ( errorRoute ) ;
34
- if ( em ) {
35
- // store.openModal("signup");
36
- document . location . hash = '' ;
39
+ // earliest possible bail on any match
40
+ if ( hash . match ( errorRoute ) ) {
41
+ return {
42
+ ...defaultParam ,
43
+ error : 'access_denied' ,
44
+ status : 403 ,
45
+ } ;
37
46
}
38
- const params = { } as { [ key : string ] : string } ;
39
- hash . split ( '&' ) . forEach ( pair => {
40
- const [ key , value ] = pair . split ( '=' ) ;
41
- params [ key ] = value ;
42
- } ) ;
43
47
44
- const am = hash . match ( accessTokenRoute ) ;
45
- if ( am ) {
46
- if ( ! ! document && params . access_token ) {
47
- document . cookie = `nf_jwt=${ params . access_token } ` ;
48
+ const matchesActionHashes = hash . match ( routes ) ;
49
+
50
+ if ( matchesActionHashes ) {
51
+ const params = reduceHashToKeyValue ( hash ) ;
52
+
53
+ if ( params . confirmation_token ) {
54
+ gotrue
55
+ . confirm ( params . confirmation_token )
56
+ . then ( setUser )
57
+ . catch ( console . error ) ;
58
+
59
+ // dont notify dev as this package does not export its own method for this
60
+ return defaultParam ;
48
61
}
49
- document . location . hash = '' ;
50
- // store.openModal("login");
51
- // store.completeExternalLogin(params);
52
- gotrue
53
- . createUser ( params , remember )
54
- . then ( setUser )
55
- . catch ( console . error ) ;
56
- }
57
62
58
- const cm = hash . match ( confirmationRoute ) ;
59
- if ( cm ) {
60
- document . location . hash = '' ;
61
- // store.openModal("login");
62
- // store.completeExternalLogin(params);
63
- gotrue
64
- . confirm ( params . confirmation_token )
65
- . then ( setUser )
66
- . catch ( console . error ) ;
67
- }
63
+ if ( params . access_token ) {
64
+ document . cookie = `nf_jwt=${ params . access_token } ` ;
68
65
69
- if ( m ) {
70
- return {
71
- ...defaultParam ,
72
- type : m [ 1 ] as TokenParam [ 'type' ] ,
73
- token : m [ 2 ] ,
74
- } ;
75
- }
66
+ gotrue
67
+ . createUser ( params , remember )
68
+ . then ( setUser )
69
+ . catch ( console . error ) ;
70
+
71
+ // also dont notify dev here for the same reasons as above
72
+ return defaultParam ;
73
+ }
76
74
77
- if ( em ) {
75
+ // pass responsibility to dev in all other cases
78
76
return {
79
77
...defaultParam ,
80
- error : 'access_denied' ,
81
- status : 403 ,
78
+ type : matchesActionHashes [ 1 ] as TokenParam [ 'type' ] ,
79
+ token : matchesActionHashes [ 2 ] ,
82
80
} ;
83
81
}
84
82
0 commit comments