-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.server.ts
53 lines (45 loc) · 1.37 KB
/
auth.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
import { Authenticator } from "remix-auth";
import type { GoogleSession } from "./cookie.server";
import { createSessionStorage } from "./cookie.server";
import {
GoogleStrategy,
GoogleStrategyDefaultName,
createGoogleStrategy,
} from "./google-strategy.server";
import type { ServerArgs } from "./types";
import type { AppLoadContext } from "@remix-run/cloudflare";
import { redirect } from "@remix-run/cloudflare";
import { route } from "routes-gen";
function createAuthenticator(args: ServerArgs) {
const authenticator = new Authenticator<GoogleSession>(
createSessionStorage(args.context.env),
GoogleStrategy.authenticatorOptions,
);
authenticator.use(createGoogleStrategy(args));
return authenticator;
}
export function authenticate(args: ServerArgs) {
return createAuthenticator(args).authenticate(
GoogleStrategyDefaultName,
args.request,
{
successRedirect: route("/"),
failureRedirect: route("/login"),
},
);
}
export function logout(args: ServerArgs) {
return createAuthenticator(args).logout(args.request, {
redirectTo: route("/login"),
});
}
export function requireUser(context: AppLoadContext) {
const user = context.user;
if (!user) {
throw redirect(route("/login"));
}
return user;
}
export async function getUserSession(args: ServerArgs) {
return createAuthenticator(args).isAuthenticated(args.request);
}