-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathAuthProvider.ts
More file actions
166 lines (146 loc) · 4.67 KB
/
AuthProvider.ts
File metadata and controls
166 lines (146 loc) · 4.67 KB
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
// import * as firebase from "firebase";
import { FirebaseAuth } from "@firebase/auth-types";
import {
AUTH_LOGIN,
AUTH_LOGOUT,
AUTH_ERROR,
AUTH_CHECK,
AUTH_GET_PERMISSIONS
} from "react-admin";
import { log, CheckLogging } from "../misc/logger";
import { RAFirebaseOptions } from "./RAFirebaseOptions";
import { FirebaseWrapper } from "./database/firebase/FirebaseWrapper";
class AuthClient {
private auth: FirebaseAuth;
constructor(firebaseConfig: {}, options: RAFirebaseOptions) {
log("Auth Client: initializing...", { firebaseConfig, options });
const fireWrapper = new FirebaseWrapper();
fireWrapper.init(firebaseConfig, options);
this.auth = fireWrapper.auth();
}
public async HandleAuthLogin(params) {
const { username, password, mode } = params;
let user;
try {
if (mode === "link") {
if (this.auth.isSignInWithEmailLink(window.location.href)) {
user = await this.auth.signInWithEmailLink(
username,
window.location.href
);
// Save email LocalStorage for same browser login
window.localStorage.removeItem("emailForSignIn");
// Clear Search Params
const urlWithoutParams =
window.location.origin +
window.location.pathname +
window.location.hash;
window.history.replaceState({}, document.title, urlWithoutParams);
log("HandleAuthLogin: user sucessfully logged in", { user });
} else {
const result: any = await this.auth.sendSignInLinkToEmail(username, {
url: window.location.href,
handleCodeInApp: true
});
// Clear LocalStorage
window.localStorage.setItem("emailForSignIn", username);
log("HandleAuthLogin: login link sucessfully requested", {
user: result && result.user
});
}
} else {
// Regular password login
user = await this.auth.signInWithEmailAndPassword(username, password);
log("HandleAuthLogin: user sucessfully logged in", { user });
}
} catch (e) {
log(`HandleAuthLogin: invalid credentials Error ${e}`, { params });
throw new Error("Login error: invalid credentials");
}
}
public async HandleAuthLogout(params) {
await this.auth.signOut();
}
public async HandleAuthError(params) {}
public async HandleAuthCheck(params) {
try {
const user = await this.getUserLogin();
log("HandleAuthCheck: user is still logged in", { user });
} catch (e) {
log("HandleAuthCheck: ", { e });
throw new Error("Auth check error: " + e);
}
}
public async getUserLogin() {
return new Promise((resolve, reject) => {
this.auth.onAuthStateChanged(user => {
if (user) {
resolve(user);
} else {
reject("User not logged in");
}
});
});
}
public async HandleGetCurrent() {
try {
const user = await this.getUserLogin();
log("HandleGetCurrent: current user", { user });
return user;
} catch (e) {
log("HandleGetCurrent: no user is logged in", { e });
return null;
}
}
public async HandleGetPermissions() {
try {
const user = await this.getUserLogin();
// @ts-ignore
const token = await user.getIdTokenResult();
return token.claims;
} catch (e) {
log("HandleGetPermission: no user is logged in or tokenResult error", {
e
});
return null;
}
}
}
export function AuthProvider(firebaseConfig: {}, options: RAFirebaseOptions) {
VerifyAuthProviderArgs(firebaseConfig, options);
const auth = new AuthClient(firebaseConfig, options);
CheckLogging(firebaseConfig, options);
return async (type: string, params: {}) => {
log("Auth Event: ", { type, params });
{
switch (type) {
case AUTH_LOGIN:
return auth.HandleAuthLogin(params);
case AUTH_LOGOUT:
return auth.HandleAuthLogout(params);
case AUTH_ERROR:
return auth.HandleAuthError(params);
case AUTH_CHECK:
return auth.HandleAuthCheck(params);
case "AUTH_GETCURRENT":
return auth.HandleGetCurrent();
case AUTH_GET_PERMISSIONS:
return auth.HandleGetPermissions();
default:
throw new Error("Unhandled auth type:" + type);
}
}
};
}
function VerifyAuthProviderArgs(
firebaseConfig: {},
options: RAFirebaseOptions
) {
const hasNoApp = !options || !options.app;
const hasNoConfig = !firebaseConfig;
if (hasNoConfig && hasNoApp) {
throw new Error(
"Please pass the Firebase firebaseConfig object or options.app to the FirebaseAuthProvider"
);
}
}