-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageql.config.js
68 lines (60 loc) · 2.02 KB
/
pageql.config.js
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
68
import { auth } from "./src/js/nhost.js";
import { initClient, getClient } from "./src/js/graphqlClient.js";
export default {
auth: {
checkRole: (requiredRole) => {
//Used by pageql:authRole directive
//Return false if user's role fails to hit specifications
//Returning false hides the component
return !window.pageql.user.role.includes(requiredRole);
},
doLogin: async (loginCallback, event) => {
//Used by pageql:authLogin directive
//Can get value of "password" input with event.target.password.value
//Do login logic here
try {
await auth.login(event.target.email.value, event.target.password.value);
window.pageql.user = {
userId: auth.getClaim("x-hasura-user-id"),
role: ["admin"] //Get from login response
};
window.pageql.authState.set({ loggedIn: true });
initClient();
} catch (error) {
console.log(error)
window.pageql.authState.set({ loggedIn: false });
}
loginCallback();
},
doLogout: async (logoutCallback, event) => {
//Used by pageql:authLogout directive
try {
auth.logout();
window.pageql.authState.set({ loggedIn: false });
logoutCallback();
} catch (error) {
console.log(error)
}
},
doRegister: (registerCallback, event) => {
//Used by pageql:authRegister directive
//Can get value of "password" input with event.target.password.value
//Do register logic here
window.pageql.user = {
name: event.target.email.value,
role: ["admin"], //Get from login response
};
window.pageql.authState.set({ loggedIn: true });
registerCallback();
},
getUserId: () => {
//Custom functions like this can be defined and accessed anywhere with window.pageql.auth.getUserId()
return auth.getClaim("x-hasura-user-id");;
},
getTenantId: () => {
//You can remove this if you're not using multi tenant!
return "1";
},
},
getClient,
};