-
Notifications
You must be signed in to change notification settings - Fork 10
/
firebaseUserStore.js
117 lines (95 loc) · 3.32 KB
/
firebaseUserStore.js
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
import { writable } from 'svelte/store';
import GoTrue from 'gotrue-js';
import { navigate } from "svelte-routing";
import * as firebas2e from "firebase/app";
let firebase = firebas2e.default
import "firebase/auth";
//import "firebase/analytics";
//import firebase from "firebase";
var firebaseConfig = {
apiKey: "AIzaSyDgkLmjsLTLO8cnEhaZu-0o12wpdisCn5w",
authDomain: "svelte-fullstack-starter.firebaseapp.com",
databaseURL: "https://svelte-fullstack-starter.firebaseio.com",
projectId: "svelte-fullstack-starter",
storageBucket: "svelte-fullstack-starter.appspot.com",
messagingSenderId: "684795141693",
appId: "1:684795141693:web:bb22a3283361cfc381d454",
measurementId: "G-Y1SRV3FGND"
};
// Initialize Firebase
//firebase.initializeApp(firebaseConfig);
firebase.initializeApp(firebaseConfig);
//firebase.default.analytics();
const url = 'https://svelte-netlify-identity.netlify.com/'
const goTrueInstance =
new GoTrue({
APIUrl: `${url}/.netlify/identity`,
setCookie: true,
})
const goTrueUser = goTrueInstance.currentUser() || undefined
export const authUserStore = writable(goTrueUser);
export function logout () {
goTrueUser.logout().then(() => {
console.log(authUserStore)
authUserStore.update((user) => undefined)
navigate("/", { replace: true });
}).catch((e) => {
alert(e.message)
});
}
export async function updateUserSecuritySettings (email, password) {
try {
const updatedUser = await goTrueUser.update({ email: email, password: password })
console.log(updatedUser)
authUserStore.update(() => updatedUser)
} catch (e) {
alert(e.message)
}
}
export async function updateUserCustomSettings (fullname) {
try {
const updatedUser = await goTrueUser.update({ data: { fullname: fullname } })
console.log(updatedUser)
authUserStore.update(() => updatedUser)
} catch (e) {
alert(e.message)
}
}
export async function signin (email, password) {
try {
await goTrueInstance.login(email, password, true).then(user => {
authUserStore.update(() => user)
window.location.assign("/");
})
} catch (e) {
alert(e.message)
throw e.message
}
}
export function register (email, password) {
return firebase.auth().createUserWithEmailAndPassword(email, password)
}
export function requestPasswordRecovery (email) {
return goTrueInstance.requestPasswordRecovery(email)
}
export function confirm (token) {
goTrueInstance.confirm(token)
.then(function (response) {
alert("Account confirmed! Welcome to the party! You can now login with your details", JSON.stringify({ response }));
})
.catch(function (e) {
alert(e.message);
});
}
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);
}
}