Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 46 additions & 11 deletions src/providers/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,55 @@ class AuthClient {
private auth: FirebaseAuth;

constructor(firebaseConfig: {}, options: RAFirebaseOptions) {
log("Auth Client: initializing...", {firebaseConfig, options});
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 } = params;
const { username, password, mode } = params;
let user;

try {
const user = await this.auth.signInWithEmailAndPassword(
username,
password
);
log("HandleAuthLogin: user sucessfully logged in", { user });
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", { params });
log(`HandleAuthLogin: invalid credentials Error ${e}`, { params });
throw new Error("Login error: invalid credentials");
}
}
Expand All @@ -41,7 +73,7 @@ class AuthClient {
await this.auth.signOut();
}

public async HandleAuthError(params) { }
public async HandleAuthError(params) {}

public async HandleAuthCheck(params) {
try {
Expand All @@ -55,7 +87,7 @@ class AuthClient {

public async getUserLogin() {
return new Promise((resolve, reject) => {
this.auth.onAuthStateChanged((user) => {
this.auth.onAuthStateChanged(user => {
if (user) {
resolve(user);
} else {
Expand Down Expand Up @@ -120,7 +152,10 @@ export function AuthProvider(firebaseConfig: {}, options: RAFirebaseOptions) {
};
}

function VerifyAuthProviderArgs(firebaseConfig: {}, options: RAFirebaseOptions) {
function VerifyAuthProviderArgs(
firebaseConfig: {},
options: RAFirebaseOptions
) {
const hasNoApp = !options || !options.app;
const hasNoConfig = !firebaseConfig;
if (hasNoConfig && hasNoApp) {
Expand Down