Skip to content

Commit 1bdfaf9

Browse files
committed
feat(TokenParam): progress on TokenParam
1 parent 6e62b7e commit 1bdfaf9

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

src/index.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import GoTrue, {
1515
Settings as GoTrueSettings,
1616
} from 'gotrue-js';
1717
import { runRoutes } from './runRoutes';
18+
import { TokenParam, defaultParam } from './token';
1819

1920
type authChangeParam = (user?: User) => string | void;
2021

@@ -70,6 +71,7 @@ export type ReactNetlifyIdentityAPI = {
7071
loginProvider: (provider: Provider) => void;
7172
acceptInviteExternalUrl: (provider: Provider, token: string) => string;
7273
settings: Settings;
74+
param: TokenParam;
7375
};
7476

7577
const [_useIdentityContext, _IdentityCtxProvider] = createCtx<
@@ -126,9 +128,15 @@ export function useNetlifyIdentity(
126128
return _user; // so that we can continue chaining
127129
};
128130

131+
const [param, setParam] = useState<TokenParam>(defaultParam);
132+
129133
useEffect(() => {
130134
if (enableRunRoutes) {
131-
runRoutes(goTrueInstance, _setUser);
135+
const param = runRoutes(goTrueInstance, _setUser);
136+
137+
if (param.token || param.error) {
138+
setParam(param);
139+
}
132140
}
133141
}, []);
134142

@@ -224,6 +232,7 @@ export function useNetlifyIdentity(
224232
loginProvider,
225233
acceptInviteExternalUrl,
226234
settings,
235+
param,
227236
};
228237
}
229238

src/runRoutes.tsx

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import GoTrue, { User } from 'gotrue-js';
2+
import { TokenParam, defaultParam } from './token';
23

34
/**
45
* This code runs on every rerender so keep it light
@@ -15,9 +16,13 @@ export function runRoutes(
1516
gotrue: GoTrue,
1617
setUser: (value: User) => User | undefined,
1718
remember = true
18-
) {
19-
const hash = (document.location.hash || '').replace(/^#\/?/, '');
20-
if (!hash) return; // early terminate if no hash
19+
): TokenParam {
20+
// early terminate if no hash
21+
if (!document?.location?.hash) {
22+
return defaultParam;
23+
}
24+
25+
const hash = document.location.hash.replace(/^#\/?/, '');
2126

2227
const m = hash.match(routes);
2328
if (m) {
@@ -38,8 +43,8 @@ export function runRoutes(
3843

3944
const am = hash.match(accessTokenRoute);
4045
if (am) {
41-
if (!!document && params['access_token']) {
42-
document.cookie = `nf_jwt=${params['access_token']}`;
46+
if (!!document && params.access_token) {
47+
document.cookie = `nf_jwt=${params.access_token}`;
4348
}
4449
document.location.hash = '';
4550
// store.openModal("login");
@@ -56,8 +61,26 @@ export function runRoutes(
5661
// store.openModal("login");
5762
// store.completeExternalLogin(params);
5863
gotrue
59-
.confirm(params['confirmation_token'])
64+
.confirm(params.confirmation_token)
6065
.then(setUser)
6166
.catch(console.error);
6267
}
68+
69+
if (m) {
70+
return {
71+
...defaultParam,
72+
type: m[1] as TokenParam['type'],
73+
token: m[2],
74+
};
75+
}
76+
77+
if (em) {
78+
return {
79+
...defaultParam,
80+
error: 'access_denied',
81+
status: 403,
82+
};
83+
}
84+
85+
return defaultParam;
6386
}

src/token.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export type TokenParam = {
2+
token: string | undefined;
3+
type:
4+
| 'confirmation'
5+
| 'invite'
6+
| 'recovery'
7+
| 'email_change'
8+
| 'access'
9+
| 'confirmation'
10+
| undefined;
11+
error?: 'access_denied';
12+
status?: 403;
13+
};
14+
15+
export const defaultParam: TokenParam = {
16+
token: undefined,
17+
type: undefined,
18+
};

0 commit comments

Comments
 (0)