Skip to content

Commit 2f9922a

Browse files
committed
Rest of the manage snippets
1 parent 01c3aa9 commit 2f9922a

File tree

6 files changed

+251
-0
lines changed

6 files changed

+251
-0
lines changed

auth-next/manage.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,99 @@ function updateUserEmail() {
7070
});
7171
// [END auth_update_user_email]
7272
}
73+
74+
function sendEmailVerification() {
75+
// [START send_email_verification]
76+
const { getAuth, sendEmailVerification } = require("firebase/auth");
77+
78+
const auth = getAuth();
79+
const user = auth.currentUser;
80+
81+
sendEmailVerification(user).then(() => {
82+
// Email sent.
83+
}).catch((error) => {
84+
// An error ocurred
85+
// ...
86+
});
87+
// [END send_email_verification]
88+
}
89+
90+
function updatePassword() {
91+
function getASecureRandomPassword() {
92+
return "correcthorsebatterystaple";
93+
}
94+
95+
// [START auth_update_password]
96+
const { getAuth, updatePassword } = require("firebase/auth");
97+
98+
const auth = getAuth();
99+
100+
const user = auth.currentUser;
101+
const newPassword = getASecureRandomPassword();
102+
103+
updatePassword(user, newPassword).then(() => {
104+
// Update successful.
105+
}).catch((error) => {
106+
// An error ocurred
107+
// ...
108+
});
109+
// [END auth_update_password]
110+
}
111+
112+
function sendPasswordReset() {
113+
// [START auth_send_password_reset]
114+
const { getAuth, sendPasswordResetEmail } = require("firebase/auth");
115+
116+
const auth = getAuth();
117+
const emailAddress = "user@example.com";
118+
119+
sendPasswordResetEmail(auth, emailAddress).then(() => {
120+
// Email sent.
121+
}).catch((error) => {
122+
// An error ocurred
123+
// ...
124+
});
125+
// [END auth_send_password_reset]
126+
}
127+
128+
function deleteUser() {
129+
// [START auth_delete_user]
130+
const { getAuth, deleteUser } = require("firebase/auth");
131+
132+
const auth = getAuth();
133+
const user = auth.currentUser;
134+
135+
deleteUser(user).then(() => {
136+
// User deleted.
137+
}).catch((error) => {
138+
// An error ocurred
139+
// ...
140+
});
141+
// [END auth_delete_user]
142+
}
143+
144+
function reauthenticateWithCredential() {
145+
/**
146+
* @returns {object}
147+
*/
148+
function promptForCredentials() {
149+
return {};
150+
}
151+
152+
// [START auth_reauth_with-credential]
153+
const { getAuth, reauthenticateWithCredential } = require("firebase/auth");
154+
155+
const auth = getAuth();
156+
const user = auth.currentUser;
157+
158+
// TODO(you): prompt the user to re-provide their sign-in credentials
159+
const credential = promptForCredentials();
160+
161+
reauthenticateWithCredential(user, credential).then(() => {
162+
// User re-authenticated.
163+
}).catch((error) => {
164+
// An error ocurred
165+
// ...
166+
});
167+
// [END auth_reauth_with-credential]
168+
}

auth/manage.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,84 @@ function updateUserEmail() {
6868
});
6969
// [END auth_update_user_email]
7070
}
71+
72+
function sendEmailVerification() {
73+
// [START send_email_verification]
74+
const user = firebase.auth().currentUser;
75+
76+
user.sendEmailVerification().then(() => {
77+
// Email sent.
78+
}).catch((error) => {
79+
// An error ocurred
80+
// ...
81+
});
82+
// [END send_email_verification]
83+
}
84+
85+
function updatePassword() {
86+
function getASecureRandomPassword() {
87+
return "correcthorsebatterystaple";
88+
}
89+
90+
// [START auth_update_password]
91+
const user = firebase.auth().currentUser;
92+
const newPassword = getASecureRandomPassword();
93+
94+
user.updatePassword(newPassword).then(() => {
95+
// Update successful.
96+
}).catch((error) => {
97+
// An error ocurred
98+
// ...
99+
});
100+
// [END auth_update_password]
101+
}
102+
103+
function sendPasswordReset() {
104+
// [START auth_send_password_reset]
105+
const auth = firebase.auth();
106+
const emailAddress = "user@example.com";
107+
108+
auth.sendPasswordResetEmail(emailAddress).then(() => {
109+
// Email sent.
110+
}).catch((error) => {
111+
// An error ocurred
112+
// ...
113+
});
114+
// [END auth_send_password_reset]
115+
}
116+
117+
function deleteUser() {
118+
// [START auth_delete_user]
119+
const user = firebase.auth().currentUser;
120+
121+
user.delete().then(() => {
122+
// User deleted.
123+
}).catch((error) => {
124+
// An error ocurred
125+
// ...
126+
});
127+
// [END auth_delete_user]
128+
}
129+
130+
function reauthenticateWithCredential() {
131+
/**
132+
* @returns {object}
133+
*/
134+
function promptForCredentials() {
135+
return {};
136+
}
137+
138+
// [START auth_reauth_with-credential]
139+
const user = firebase.auth().currentUser;
140+
141+
// TODO(you): prompt the user to re-provide their sign-in credentials
142+
const credential = promptForCredentials();
143+
144+
user.reauthenticateWithCredential(credential).then(() => {
145+
// User re-authenticated.
146+
}).catch((error) => {
147+
// An error ocurred
148+
// ...
149+
});
150+
// [END auth_reauth_with-credential]
151+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/manage.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START auth_delete_user_modular]
7+
import { getAuth, deleteUser } from "firebase/auth";
8+
9+
const auth = getAuth();
10+
const user = auth.currentUser;
11+
12+
deleteUser(user).then(() => {
13+
// User deleted.
14+
}).catch((error) => {
15+
// An error ocurred
16+
// ...
17+
});
18+
// [END auth_delete_user_modular]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/manage.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START auth_send_password_reset_modular]
7+
import { getAuth, sendPasswordResetEmail } from "firebase/auth";
8+
9+
const auth = getAuth();
10+
const emailAddress = "user@example.com";
11+
12+
sendPasswordResetEmail(auth, emailAddress).then(() => {
13+
// Email sent.
14+
}).catch((error) => {
15+
// An error ocurred
16+
// ...
17+
});
18+
// [END auth_send_password_reset_modular]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/manage.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START auth_update_password_modular]
7+
import { getAuth, updatePassword } from "firebase/auth";
8+
9+
const auth = getAuth();
10+
11+
const user = auth.currentUser;
12+
const newPassword = getASecureRandomPassword();
13+
14+
updatePassword(user, newPassword).then(() => {
15+
// Update successful.
16+
}).catch((error) => {
17+
// An error ocurred
18+
// ...
19+
});
20+
// [END auth_update_password_modular]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./auth-next/manage.js
3+
//
4+
// To make edits to the snippets in this file, please edit the source
5+
6+
// [START send_email_verification_modular]
7+
import { getAuth, sendEmailVerification } from "firebase/auth";
8+
9+
const auth = getAuth();
10+
const user = auth.currentUser;
11+
12+
sendEmailVerification(user).then(() => {
13+
// Email sent.
14+
}).catch((error) => {
15+
// An error ocurred
16+
// ...
17+
});
18+
// [END send_email_verification_modular]

0 commit comments

Comments
 (0)