forked from rphlmr/supa-fly-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.server.ts
206 lines (178 loc) · 5.69 KB
/
session.server.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { createCookieSessionStorage, redirect } from "@remix-run/node";
import {
getCurrentPath,
isGet,
makeRedirectToFromHere,
NODE_ENV,
safeRedirect,
SESSION_SECRET,
} from "~/utils";
import { refreshAccessToken, verifyAuthSession } from "./service.server";
import type { AuthSession } from "./types";
const SESSION_KEY = "authenticated";
const SESSION_ERROR_KEY = "error";
const SESSION_MAX_AGE = 60 * 60 * 24 * 7; // 7 days;
const LOGIN_URL = "/login";
const REFRESH_ACCESS_TOKEN_THRESHOLD = 60 * 10; // 10 minutes left before token expires
/**
* Session storage CRUD
*/
const sessionStorage = createCookieSessionStorage({
cookie: {
name: "__authSession",
httpOnly: true,
path: "/",
sameSite: "lax",
secrets: [SESSION_SECRET],
secure: NODE_ENV === "production",
},
});
export async function createAuthSession({
request,
authSession,
redirectTo,
}: {
request: Request;
authSession: AuthSession;
redirectTo: string;
}) {
return redirect(safeRedirect(redirectTo), {
headers: {
"Set-Cookie": await commitAuthSession(request, {
authSession,
flashErrorMessage: null,
}),
},
});
}
async function getSession(request: Request) {
const cookie = request.headers.get("Cookie");
return sessionStorage.getSession(cookie);
}
export async function getAuthSession(
request: Request
): Promise<AuthSession | null> {
const session = await getSession(request);
return session.get(SESSION_KEY);
}
export async function commitAuthSession(
request: Request,
{
authSession,
flashErrorMessage,
}: {
authSession?: AuthSession | null;
flashErrorMessage?: string | null;
} = {}
) {
const session = await getSession(request);
// allow user session to be null.
// useful you want to clear session and display a message explaining why
if (authSession !== undefined) {
session.set(SESSION_KEY, authSession);
}
session.flash(SESSION_ERROR_KEY, flashErrorMessage);
return sessionStorage.commitSession(session, { maxAge: SESSION_MAX_AGE });
}
export async function destroyAuthSession(request: Request) {
const session = await getSession(request);
return redirect("/", {
headers: {
"Set-Cookie": await sessionStorage.destroySession(session),
},
});
}
async function assertAuthSession(
request: Request,
{ onFailRedirectTo }: { onFailRedirectTo?: string } = {}
) {
const authSession = await getAuthSession(request);
// If there is no user session, Fly, You Fools! 🧙♂️
if (!authSession?.accessToken || !authSession?.refreshToken) {
throw redirect(
`${onFailRedirectTo || LOGIN_URL}?${makeRedirectToFromHere(request)}`,
{
headers: {
"Set-Cookie": await commitAuthSession(request, {
authSession: null,
flashErrorMessage: "no-user-session",
}),
},
}
);
}
return authSession;
}
/**
* Assert auth session is present and verified from supabase auth api
*
* If used in loader (GET method)
* - Refresh tokens if session is expired
* - Return auth session if not expired
* - Destroy session if refresh token is expired
*
* If used in action (POST method)
* - Try to refresh session if expired and return this new session (it's your job to handle session commit)
* - Return auth session if not expired
* - Destroy session if refresh token is expired
*/
export async function requireAuthSession(
request: Request,
{
onFailRedirectTo,
verify,
}: { onFailRedirectTo?: string; verify: boolean } = { verify: false }
): Promise<AuthSession> {
// hello there
const authSession = await assertAuthSession(request, {
onFailRedirectTo,
});
// ok, let's challenge its access token.
// by default, we don't verify the access token from supabase auth api to save some time
const isValidSession = verify ? await verifyAuthSession(authSession) : true;
// damn, access token is not valid or expires soon
// let's try to refresh, in case of 🧐
if (!isValidSession || isExpiringSoon(authSession.expiresAt)) {
return refreshAuthSession(request);
}
// finally, we have a valid session, let's return it
return authSession;
}
function isExpiringSoon(expiresAt: number) {
return (expiresAt - REFRESH_ACCESS_TOKEN_THRESHOLD) * 1000 < Date.now();
}
async function refreshAuthSession(request: Request): Promise<AuthSession> {
const authSession = await getAuthSession(request);
const refreshedAuthSession = await refreshAccessToken(
authSession?.refreshToken
);
// 👾 game over, log in again
// yes, arbitrary, but it's a good way to don't let an illegal user here with an expired token
if (!refreshedAuthSession) {
const redirectUrl = `${LOGIN_URL}?${makeRedirectToFromHere(request)}`;
// here we throw instead of return because this function promise a AuthSession and not a response object
// https://remix.run/docs/en/v1/guides/constraints#higher-order-functions
throw redirect(redirectUrl, {
headers: {
"Set-Cookie": await commitAuthSession(request, {
authSession: null,
flashErrorMessage: "fail-refresh-auth-session",
}),
},
});
}
// refresh is ok and we can redirect
if (isGet(request)) {
// here we throw instead of return because this function promise a UserSession and not a response object
// https://remix.run/docs/en/v1/guides/constraints#higher-order-functions
throw redirect(getCurrentPath(request), {
headers: {
"Set-Cookie": await commitAuthSession(request, {
authSession: refreshedAuthSession,
}),
},
});
}
// we can't redirect because we are in an action, so, deal with it and don't forget to handle session commit 👮♀️
return refreshedAuthSession;
}