Skip to content

Commit

Permalink
🔐 auth and recovery fix
Browse files Browse the repository at this point in the history
  • Loading branch information
quantuminformation committed Dec 25, 2019
1 parent 439578b commit a9a10d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/model/User.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// todo temping to switch to TS

export function userFromFireBase (serverUser) {
return getUser(serverUser.user.email)
return getUser(serverUser.email)
}

export function userFromNetlify (serverUser) {
Expand Down
73 changes: 37 additions & 36 deletions src/stores/firebaseUserStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,38 @@ var firebaseConfig = {
// Initialize Firebase
//firebase.initializeApp(firebaseConfig);
firebase.initializeApp(firebaseConfig);
//firebase.default.analytics();
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)

// firebase specific
let userLoaded = false;

function getCurrentUser () {
return new Promise((resolve, reject) => {
if (userLoaded) {
resolve(firebase.auth().currentUser);
}
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
userLoaded = true;
unsubscribe();
resolve(user);
}, reject);
});
}

//const user = firebase.auth().currentUser()
export let authUserStore = writable(null);

async function init () {
let user = await getCurrentUser()
user && authUserStore.update(store => userFromFireBase(user));
}

//todo check user
export const authUserStore = writable(null);
init()

//todo
export async function logout () {

export function logout () {
goTrueUser.logout().then(() => {
// let user = await getCurrentUser()
firebase.auth().signOut().then(() => {
console.log(authUserStore)
authUserStore.update((user) => undefined)
navigate("/", { replace: true });
Expand Down Expand Up @@ -72,19 +93,17 @@ export async function updateUserCustomSettings (fullname) {

export async function signin (email, password) {
try {
await firebase.auth().signInWithEmailAndPassword(email, password).then(user => {
let {user} = await firebase.auth().signInWithEmailAndPassword(email, password) // sometimes the user is wrapped in user, vs get currentUser which isn't wrapped
if (user.emailVerified) {

if (user.user.emailVerified) {
authUserStore.update(() => userFromFireBase(user))
navigate("/", { replace: true });

authUserStore.update(() => userFromFireBase(user))
navigate("/", { replace: true });

} else {
alert('User not found, if you registered before, have you checked your email?')
throw new Error() // no message needed for the UI to stop the spinner
}
console.log(user)
})
} else {
alert('User not found, if you registered before, have you checked your email?')
throw new Error() // no message needed for the UI to stop the spinner
}
console.log(user)
} catch (e) {
alert(e.message)
throw e.message
Expand All @@ -103,25 +122,7 @@ export async function register (email, password) {

}

//todo
export function requestPasswordRecovery (email) {
return goTrueInstance.requestPasswordRecovery(email)
return firebase.auth().sendPasswordResetEmail(email)
}

//todo

export async function recover (token) {
try {

let existingUser = await goTrueInstance.recover(token)

alert("Account recovered! You are now logged in. Please change your password immediately by updating your security settings.", JSON.stringify({ response }));
console.log(`recovered account: ${existingUser}`)
authUserStore.update(() => existingUser)
window.location.assign("/settings");
} catch (e) {
console.log('something wrong with recovery')
alert(e.message);
}

}

0 comments on commit a9a10d9

Please sign in to comment.