forked from refinedev/refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthProvider.ts
164 lines (148 loc) · 3.61 KB
/
authProvider.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import type { AuthProvider } from "@refinedev/core";
import Cookies from "js-cookie";
import * as cookie from "cookie";
const mockUsers = [
{
roles: ["admin"],
},
{
roles: ["editor"],
},
{
roles: ["user"],
},
];
const COOKIE_NAME = "user";
export const authProvider: AuthProvider = {
login: async ({ email }) => {
// Suppose we actually send a request to the back end here.
const user = mockUsers.find((item) => item.email === email);
if (user) {
Cookies.set(COOKIE_NAME, JSON.stringify(user));
return {
success: true,
redirectTo: "/",
};
}
return {
success: false,
error: {
message: "Login failed",
name: "Invalid email or password",
},
};
},
register: async (params) => {
// Suppose we actually send a request to the back end here.
const user = mockUsers.find((item) => item.email === params.email);
if (user) {
Cookies.set(COOKIE_NAME, JSON.stringify(user));
return {
success: true,
redirectTo: "/",
};
}
return {
success: false,
error: {
message: "Register failed",
name: "Invalid email or password",
},
};
},
forgotPassword: async (params) => {
// Suppose we actually send a request to the back end here.
const user = mockUsers.find((item) => item.email === params.email);
if (user) {
//we can send email with reset password link here
return {
success: true,
};
}
return {
success: false,
error: {
message: "Forgot password failed",
name: "Invalid email",
},
};
},
updatePassword: async (params) => {
// Suppose we actually send a request to the back end here.
const isPasswordInvalid = params.password === "123456" || !params.password;
if (isPasswordInvalid) {
return {
success: false,
error: {
message: "Update password failed",
name: "Invalid password",
},
};
}
return {
success: true,
};
},
logout: async () => {
Cookies.remove(COOKIE_NAME);
return {
success: true,
redirectTo: "/login",
};
},
onError: async (error) => {
if (error && error.statusCode === 401) {
return {
error: new Error("Unauthorized"),
logout: true,
redirectTo: "/login",
};
}
return {};
},
check: async (request) => {
let user = undefined;
if (request) {
const hasCookie = request.headers.get("Cookie");
if (hasCookie) {
const parsedCookie = cookie.parse(request.headers.get("Cookie"));
user = parsedCookie[COOKIE_NAME];
}
} else {
const parsedCookie = Cookies.get(COOKIE_NAME);
user = parsedCookie ? JSON.parse(parsedCookie) : undefined;
}
const { pathname } = new URL(request.url);
const query = pathname === "/" ? "" : `?to=${encodeURIComponent(pathname)}`;
if (!user) {
return {
authenticated: false,
error: {
message: "Check failed",
name: "Unauthorized",
},
logout: true,
redirectTo: `/login${query}`,
};
}
return {
authenticated: true,
};
},
getPermissions: async () => {
return null;
},
getIdentity: async () => {
const cookie = Cookies.get(COOKIE_NAME);
if (!cookie) return null;
return {
id: 1,
name: "Jane Doe",
avatar:
"https://unsplash.com/photos/IWLOvomUmWU/download?force=true&w=640",
};
},
};