Skip to content

Commit 735cda9

Browse files
author
Alexandru Comanescu
committed
Implement skip auth
1 parent 3f40541 commit 735cda9

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

src/components/account-popover.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Router from 'next/router';
33
import PropTypes from 'prop-types';
44
import { Box, MenuItem, MenuList, Popover, Typography } from '@mui/material';
55
import { AuthContext } from '../contexts/auth-context';
6-
import { auth } from '../lib/auth';
6+
import { auth, ENABLE_AUTH } from '../lib/auth';
77

88
export const AccountPopover = (props) => {
99
const { anchorEl, onClose, open, ...other } = props;
@@ -12,6 +12,28 @@ export const AccountPopover = (props) => {
1212
const handleSignOut = async () => {
1313
onClose?.();
1414

15+
// Check if authentication with Zalter is enabled
16+
// If not enabled, then redirect is not required
17+
if (!ENABLE_AUTH) {
18+
return;
19+
}
20+
21+
// Check if auth has been skipped
22+
// From sign-in page we may have set "skip-auth" to "true"
23+
// If this has been skipped, then redirect to "sign-in" directly
24+
const authSkipped = globalThis.sessionStorage.getItem('skip-auth') === 'true';
25+
26+
if (authSkipped) {
27+
// Cleanup the skip auth state
28+
globalThis.sessionStorage.removeItem('skip-auth');
29+
30+
// Redirect to sign-in page
31+
Router
32+
.push('/sign-in')
33+
.catch(console.error);
34+
return;
35+
}
36+
1537
try {
1638
// This can be call inside AuthProvider component, but we do it here for simplicity
1739
await auth.signOut();

src/contexts/auth-context.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ const initialState = {
1616

1717
const handlers = {
1818
[HANDLERS.INITIALIZE]: (state, action) => {
19+
const user = action.payload;
20+
1921
return {
2022
...state,
2123
...(
2224
// if payload (user) is provided, then is authenticated
23-
action.payload
25+
user
2426
? ({
2527
isAuthenticated: true,
2628
isLoading: false,
27-
user: action.payload
29+
user
2830
})
2931
: ({
3032
isLoading: false
@@ -33,10 +35,12 @@ const handlers = {
3335
};
3436
},
3537
[HANDLERS.SIGN_IN]: (state, action) => {
38+
const user = action.payload;
39+
3640
return {
3741
...state,
3842
isAuthenticated: true,
39-
user: action.payload
43+
user
4044
};
4145
},
4246
[HANDLERS.SIGN_OUT]: (state) => {
@@ -69,12 +73,28 @@ export const AuthProvider = (props) => {
6973

7074
initialized.current = true;
7175

76+
// Check if auth has been skipped
77+
// From sign-in page we may have set "skip-auth" to "true"
78+
const authSkipped = globalThis.sessionStorage.getItem('skip-auth') === 'true';
79+
80+
if (authSkipped) {
81+
const user = {};
82+
83+
dispatch({
84+
type: HANDLERS.INITIALIZE,
85+
payload: user
86+
});
87+
return;
88+
}
89+
7290
// Check if authentication with Zalter is enabled
7391
// If not, then set user as authenticated
7492
if (!ENABLE_AUTH) {
93+
const user = {};
94+
7595
dispatch({
7696
type: HANDLERS.INITIALIZE,
77-
payload: {}
97+
payload: user
7898
});
7999
return;
80100
}
@@ -83,17 +103,19 @@ export const AuthProvider = (props) => {
83103
// Check if user is authenticated
84104
const isAuthenticated = await auth.isAuthenticated();
85105

86-
let user;
87-
88106
if (isAuthenticated) {
89107
// Get user from your database
90-
user = {};
108+
const user = {};
109+
110+
dispatch({
111+
type: HANDLERS.INITIALIZE,
112+
payload: user
113+
});
114+
} else {
115+
dispatch({
116+
type: HANDLERS.INITIALIZE
117+
});
91118
}
92-
93-
dispatch({
94-
type: HANDLERS.INITIALIZE,
95-
payload: user
96-
});
97119
} catch (err) {
98120
console.error(err);
99121
dispatch({

src/pages/sign-in/index.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import Head from 'next/head';
33
import NextLink from 'next/link';
44
import { useFormik } from 'formik';
55
import * as Yup from 'yup';
6-
import { Box, Button, Grid, Tab, Tabs, TextField, Typography } from '@mui/material';
6+
import { Box, Button, FormHelperText, Grid, Tab, Tabs, TextField, Typography } from '@mui/material';
77
import { auth, ENABLE_AUTH } from '../../lib/auth';
88
import { Logo } from '../../components/logo';
9+
import { useAuthContext } from '../../contexts/auth-context';
10+
import Router from 'next/router';
911

1012
const Page = () => {
1113
const [tab, setTab] = useState('email');
1214
const [emailSent, setEmailSent] = useState(false);
15+
const authContext = useAuthContext();
1316
const formik = useFormik({
1417
initialValues: {
1518
email: '',
@@ -57,6 +60,22 @@ const Page = () => {
5760
setEmailSent(false);
5861
};
5962

63+
const handleSkip = () => {
64+
// Since skip is requested, we set a fake user as authenticated
65+
const user = {};
66+
67+
// Update Auth Context state
68+
authContext.signIn(user);
69+
70+
// Persist the skip for AuthProvider initialize call
71+
globalThis.sessionStorage.setItem('skip-auth', 'true');
72+
73+
// Redirect to home page
74+
Router
75+
.push('/')
76+
.catch(console.error);
77+
};
78+
6079
return (
6180
<>
6281
<Head>
@@ -209,6 +228,9 @@ const Page = () => {
209228
value={formik.values.email}
210229
variant="outlined"
211230
/>
231+
<FormHelperText sx={{ mt: 1 }}>
232+
Enter a valid email since this is a fully integrated authentication system. Optionally you can skip.
233+
</FormHelperText>
212234
{formik.errors.submit && (
213235
<Typography
214236
color="error"
@@ -227,6 +249,14 @@ const Page = () => {
227249
>
228250
Continue
229251
</Button>
252+
<Button
253+
fullWidth
254+
size="large"
255+
sx={{ mt: 3 }}
256+
onClick={handleSkip}
257+
>
258+
Skip authentication
259+
</Button>
230260
</div>
231261
)}
232262
{tab === 'phoneNumber' && (

0 commit comments

Comments
 (0)