-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.ts
136 lines (125 loc) · 3.95 KB
/
auth.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
import { UnauthorizedError, User } from "@teamhanko/hanko-elements";
import { defineStore } from "statebuilder";
import { withProxyCommands } from "statebuilder/commands";
import { useNavigate } from "@solidjs/router";
import { withHanko, withHankoContext } from "./hanko";
import { createEffect, createMemo, on } from "solid-js";
import {
getSupabaseCookie,
patchSupabaseRestClient,
syncSupabaseTokenFromHankoSession,
} from "../supabase";
import { signSupabaseToken } from "../services/auth";
import { createControlledDialog } from "../utils/controlledDialog";
import { ProfileDialog } from "../../components/Auth/ProfileDialog";
type AuthCommands = {
setCurrent: User | null;
setLoading: boolean;
setReady: boolean;
setSupabaseAccessToken: string | null;
forceLogout: void;
};
interface State {
user: User | null;
loading: boolean;
ready: boolean;
supabaseAccessToken: string | null;
}
const enableAuthMock = import.meta.env.VITE_ENABLE_AUTH_MOCK;
function initialState() {
return {
loading: false,
ready: false,
supabaseAccessToken: null,
user: null,
};
}
export const AuthState = defineStore<State>(initialState)
.extend(withHankoContext(enableAuthMock))
.extend(withProxyCommands<AuthCommands>({ devtools: { storeName: "auth" } }))
.extend(withHanko())
.extend((_) => {
_.hold(_.commands.setCurrent, (user) => _.set("user", () => user));
_.hold(_.commands.setLoading, (loading) => _.set("loading", () => loading));
_.hold(_.commands.setReady, (ready) => _.set("ready", () => ready));
_.hold(_.commands.setSupabaseAccessToken, (token) =>
_.set("supabaseAccessToken", () => token),
);
})
.extend((_) => ({
loadCurrentUser() {
return _.getCurrentUser().catch((e) => {
if (e instanceof UnauthorizedError) {
return null;
}
});
},
}))
.extend((_, context) => {
const navigate = useNavigate();
const loggedIn = () => !!_.get.supabaseAccessToken && !!_();
context.hooks.onInit(() => {
_.loadCurrentUser().then((user) => {
if (!user) {
_.actions.setSupabaseAccessToken(null);
navigate("/login");
} else {
_.actions.setSupabaseAccessToken(getSupabaseCookie());
_.actions.setCurrent(user ?? null);
}
_.actions.setReady(true);
});
_.hanko.onAuthFlowCompleted(() => {
_.actions.setLoading(true);
_.loadCurrentUser().then((user) => {
_.actions.setCurrent(user ?? null);
signSupabaseToken(_.hanko.session.get())
.then(({ access_token }) =>
_.actions.setSupabaseAccessToken(access_token),
)
.then(() => _.actions.setLoading(false))
.then(() => navigate("/"))
.catch(() => {
_.actions.setLoading(false);
return _.hanko.user.logout().then(() => navigate("/login"));
});
});
});
createEffect(
on(
createMemo(() => _.get.supabaseAccessToken),
(accessToken) => {
patchSupabaseRestClient(accessToken);
syncSupabaseTokenFromHankoSession(accessToken, _.session());
},
{ defer: true },
),
);
_.hanko.onSessionCreated((session) => {
_.actions.setSupabaseAccessToken(session.jwt!);
});
_.hanko.onSessionExpired(() => {
_.actions.setSupabaseAccessToken(null);
});
_.hanko.onUserLoggedOut(() => {
_.actions.setSupabaseAccessToken(null);
});
});
const controlledDialog = createControlledDialog();
return {
goToProfile() {
controlledDialog(ProfileDialog, {});
},
loggedIn,
logout() {
return _.hanko.user
.logout()
.then(() => navigate("/login"))
.catch((e) => {
console.error("Error during logout:", e);
// TODO add toast
alert("Error while logout");
});
},
};
});