-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
122 lines (109 loc) · 2.57 KB
/
auth.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
118
119
120
121
122
import {
getAuth,
createUserWithEmailAndPassword,
onAuthStateChanged,
updateProfile,
updateEmail,
updatePassword,
} from "firebase/auth";
// Initialize Firebase Authentication
const auth = getAuth();
//Get Current User
const user = auth.currentUser;
//Criar um novo usuário com email e senha
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
//Signin Auth
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
//Signout Auth
signOut(auth)
.then(() => {
// Sign-out successful.
})
.catch((error) => {
// An error happened.
});
//Controlar os estatos de authenticação
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
const uid = user.uid;
// ...
} else {
// User is signed out
// ...
}
});
//Acessar os dados de um usuário
if (user !== null) {
// The user object has basic properties such as display name, email, etc.
const displayName = user.displayName;
const email = user.email;
const photoURL = user.photoURL;
const emailVerified = user.emailVerified;
// The user's ID, unique to the Firebase project. Do NOT use
// this value to authenticate with your backend server, if
// you have one. Use User.getToken() instead.
const uid = user.uid;
}
//Atualizar os dados de um usuário
updateProfile(auth.currentUser, {
displayName: "Jane Q. User",
photoURL: "https://example.com/jane-q-user/profile.jpg",
})
.then(() => {
// Profile updated!
// ...
})
.catch((error) => {
// An error occurred
// ...
});
//Atualizar email de um usuário
updateEmail(auth.currentUser, "user@example.com")
.then(() => {
// Email updated!
// ...
})
.catch((error) => {
// An error occurred
// ...
});
//Atualziar senha de um usuário
const newPassword = getASecureRandomPassword();
updatePassword(user, newPassword)
.then(() => {
// Update successful.
})
.catch((error) => {
// An error ocurred
// ...
});
//Deletar um usuário
deleteUser(user)
.then(() => {
// User deleted.
})
.catch((error) => {
// An error ocurred
// ...
});