This repository was archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathauth0CheckUser.tsx
More file actions
67 lines (58 loc) · 2.12 KB
/
auth0CheckUser.tsx
File metadata and controls
67 lines (58 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useUser } from '@auth0/nextjs-auth0/client';
import { useRouter } from 'next/router';
type roleAndAllowed = {
allowed: boolean,
role?: string,
}
/*
Explaination:
This function SHOULD be implemented within every route except the initial '/' route.
It checks the see if a user is logged in, otherwise it redirects them to the initial
('login') page. Also checks to see if the user is the correct role otherwise they are
redirected to the login page.
Parameter:
usersAllowed is a string array of user roles that are allowed. Example:
["Student", "Technician", "Admin"]
Returns:
True if the user is signed in and has the correct permissions, otherwise false.
Implementation:
if(!CheckUser(["Admin"])) return(<div>Redirecting...</div>);
*/
export default function CheckUser(rolesAllowed?: String[]) : roleAndAllowed {
const { user, error, isLoading } = useUser();
const router = useRouter();
if (isLoading) return <div>Loading...</div>;
if (error) return(<div>{error.message}</div>);
if (!user) {
router.push('/api/auth/login?returnTo=/');
return{
'allowed': false,
'role': null,
};
}
/*
------------------------------------------------------------------------------
INSERT CODE that connects to database and gets user's ROLE from NAME and EMAIL
------------------------------------------------------------------------------
*/
var role
// ideally this would fetch role from prsima database
if(user.email == 'guerbray@gmail.com')
role = 'Admin'
else
role = "Technician"
// if any of the users are allowed, return true and send the role
for(const roleAllowed of rolesAllowed) {
if (roleAllowed == role)
return{
'allowed': true,
'role': role,
};
}
// else, send to the login screen
router.push('/api/auth/logout');
return{
'allowed': false,
'role': null,
};
}