-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix merge conflict and some additional typos #43
base: master
Are you sure you want to change the base?
Changes from all commits
5e4fa34
4b488c1
2e69674
ea454bf
61723f7
2ea2390
0ee757a
b37b38f
45b6f3d
86991a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,10 @@ | |
/** | ||
* Object response from a "fetchProvidersForEmail" request. | ||
* @typedef {Object} ProvidersForEmailResponse | ||
* @property {Array.<string>} allProviders All providers the user has once used to do federated sign-in. | ||
* @property {boolean} registered All sign-in methods this user has used. | ||
* @property {Array.<string>} allProviders All providers the user has once used to do federated sign in. | ||
* @property {boolean} registered All sign in methods this user has used. | ||
* @property {string} sessionId Session ID which should be passed in the following verifyAssertion request. | ||
* @property {Array.<string>} signinMethods All sign-in methods this user has used. | ||
* @property {Array.<string>} signinMethods All sign in methods this user has used. | ||
*/ | ||
|
||
/** | ||
|
@@ -66,15 +66,15 @@ export default class Auth { | |
// we need to check if this environment supports `addEventListener` on the window. | ||
'addEventListener' in window && | ||
window.addEventListener('storage', e => { | ||
// This code will run if localStorage for this user | ||
// data was updated from a different browser window. | ||
// This code will run if the localStorage for this user | ||
// was updated from a different browser window. | ||
if (e.key !== this.sKey('User')) return; | ||
this.setState(JSON.parse(e.newValue), false); | ||
}); | ||
} | ||
|
||
/** | ||
* Emits an event and triggers all of the listeners. | ||
* Emits an event and triggers all the listeners. | ||
* @param {string} name Name of the event to trigger. | ||
* @param {any} data Data you want to pass to the event listeners. | ||
* @private | ||
|
@@ -122,7 +122,7 @@ export default class Auth { | |
let data = await response.json(); | ||
|
||
// If the response returned an error, try to get a Firebase error code/message. | ||
// Sometimes the error codes are joined with an explanation, we don't need that(its a bug). | ||
// Sometimes the error codes are joined with an explanation, we don't need that (it's a bug). | ||
// So we remove the unnecessary part. | ||
if (!response.ok) { | ||
const code = data.error.message.replace(/ ?: [\w ,.'"()]+$/, ''); | ||
|
@@ -137,19 +137,19 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Makes sure the user is signed-in and has up-to-date credentials. | ||
* @throws Will throw if the user is not signed-in. | ||
* Makes sure the user is signed in and has up-to-date credentials. | ||
* @throws Will throw if the user is not signed in. | ||
* @private | ||
*/ | ||
async enforceAuth() { | ||
if (!this.user) throw Error('The user must be signed-in to use this method.'); | ||
if (!this.user) throw Error('The user must be signed in to use this method.'); | ||
return this.refreshIdToken(); // Won't do anything if the token is valid. | ||
} | ||
|
||
/** | ||
* Updates the user data in localStorage. | ||
* @param {Object} userData New user data. | ||
* @param {boolean} [updateStorage = true] Check whether to update localStorage or not. | ||
* @param {boolean} [persist = true] Whether to update localStorage or not. | ||
* @private | ||
*/ | ||
async setState(userData, persist = true, emit = true) { | ||
|
@@ -159,7 +159,7 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Sign out the currently signed-in user. | ||
* Sign out the currently signed in user. | ||
* Removes all data stored in the storage that's associated with the user. | ||
*/ | ||
signOut() { | ||
|
@@ -199,7 +199,7 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Uses native fetch, but adds authorization headers, otherwise, the API is exactly the same as native fetch. | ||
* Uses native fetch but adds authorization headers, otherwise the API is exactly the same as native fetch. | ||
* @param {Request|Object|string} resource A request to send. It can be a resource or an options object. | ||
* @param {Object} init An options object. | ||
*/ | ||
|
@@ -231,7 +231,7 @@ export default class Auth { | |
|
||
/** | ||
* Starts the auth flow of a federated ID provider. | ||
* Also, it will redirect the page to the federated sign-in page. | ||
* Also, it will redirect the page to the federated sign in page. | ||
* @param {oauthFlowOptions|string} options An options object or a string with the name of the provider. | ||
*/ | ||
async signInWithProvider(options) { | ||
|
@@ -244,7 +244,7 @@ export default class Auth { | |
const { provider, oauthScope, context, linkAccount } = | ||
typeof options === 'string' ? { provider: options } : options; | ||
|
||
// Make sure the user is logged in when an "account link" was requested. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn’t need to move this comment |
||
// Makes sure the user is signed in when an "account link" was requested. | ||
linkAccount && (await this.enforceAuth()); | ||
|
||
// Get the url and other data necessary for the authentication. | ||
|
@@ -256,11 +256,11 @@ export default class Auth { | |
context | ||
}); | ||
|
||
// Save the sessionId that we just received in the local storage. | ||
// Save the sessionId that we just received in the localStorage. | ||
// Is required to finish the auth flow, I believe this is used to mitigate CSRF attacks. | ||
// (No docs on this...) | ||
await this.storage.set(this.sKey('SessionId'), sessionId); | ||
// Save if this is a fresh log-in or a "link account" request. | ||
// Save if this is a fresh sign in or a "link account" request. | ||
linkAccount && (await this.storage.set(this.sKey('LinkAccount'), true)); | ||
|
||
// Finally - redirect the page to the auth endpoint. | ||
|
@@ -278,16 +278,15 @@ export default class Auth { | |
const sessionId = await this.storage.get(this.sKey('SessionId')); | ||
// Get the indication if this was a "link account" request. | ||
const linkAccount = await this.storage.get(this.sKey('LinkAccount')); | ||
|
||
// Check for the edge case in which the user signed-out | ||
// Check for the edge case in which the user signed out | ||
// before completing the linkAccount request. | ||
if (linkAccount && !this.user) throw Error('Request to "Link account" was made, but user is no longer signed-in'); | ||
if (linkAccount && !this.user) throw Error('Request to "Link account" was made, but user is no longer signed in'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing error messages should be done separately or with a notice as some users might depend on the exact string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is important to know, I didn't know. Thanks. |
||
|
||
await this.storage.remove(this.sKey('LinkAccount')); | ||
|
||
// Try to exchange the Auth Code for an idToken and refreshToken. | ||
const { idToken, refreshToken, expiresAt, context } = await this.api('signInWithIdp', { | ||
// If this is a "link account" flow, then attach the idToken of the currently signed-in account. | ||
// If this is a "link account" flow, then attach the idToken of the currently signed in account. | ||
idToken: linkAccount ? this.user.tokenManager.idToken : undefined, | ||
requestUri, | ||
sessionId, | ||
|
@@ -304,14 +303,14 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Handles all sign-in flows that complete via redirects. | ||
* Handles all sign in flows that complete via redirects. | ||
* Fails silently if no redirect was detected. | ||
*/ | ||
async handleSignInRedirect() { | ||
// OAuth Federated Identity Provider flow. | ||
if (location.href.match(/[&?]code=/)) return this.finishProviderSignIn(); | ||
|
||
// Email sign-in flow. | ||
// Email sign in flow. | ||
if (location.href.match(/[&?]oobCode=/)) { | ||
const oobCode = location.href.match(/[?&]oobCode=([^&]+)/)[1]; | ||
const email = location.href.match(/[?&]email=([^&]+)/)[1]; | ||
|
@@ -361,8 +360,8 @@ export default class Auth { | |
|
||
/** | ||
* Sends an out-of-band confirmation code for an account. | ||
* It can be used to reset a password, to verify an email address and send a sign-in email link. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sign-in is used as a noun here, so the hyphen should remain. |
||
* The email argument is not needed if verifying an email (the argument is ignored). Otherwise, it is required. | ||
* Can be used to reset a password, to verify an email address and send a sign-in email link. | ||
* The email argument is not needed when verifying an email (it's ignored). Otherwise, it's required. | ||
* @param {'PASSWORD_RESET'|'VERIFY_EMAIL'|'EMAIL_SIGNIN'} requestType The type of out-of-band (OOB) code to send. | ||
* @param {string} [email] When the `requestType` is `PASSWORD_RESET` or `EMAIL_SIGNIN` you need to provide an email address. | ||
* @returns {Promise} | ||
|
@@ -404,9 +403,9 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Gets the user data from the server, and updates the local caches. | ||
* @param {Object} [tokenManager] Only when not signed-in. | ||
* @throws Will throw if the user is not signed-in. | ||
* Gets the user data from the server and updates the local caches. | ||
* @param {Object} [tokenManager] Only when not signed in. | ||
* @throws Will throw if the user is not signed in. | ||
DaniyelMe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
async fetchProfile(tokenManager = this.user && this.user.tokenManager) { | ||
if (!tokenManager) await this.enforceAuth(); | ||
|
@@ -420,9 +419,9 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Updates the user's profile. | ||
* Update user's profile. | ||
* @param {Object} newData An object with the new data. | ||
* @throws Will throw if the user is not signed-in. | ||
* @throws Will throw if the user is not signed in. | ||
*/ | ||
async updateProfile(newData) { | ||
await this.enforceAuth(); | ||
|
@@ -450,8 +449,8 @@ export default class Auth { | |
} | ||
|
||
/** | ||
* Deletes the currently signed-in account then sign out. | ||
* @throws Will throw if the user is not signed-in. | ||
* Deletes the currently signed in account then sign out. | ||
* @throws Will throw if the user is not signed in. | ||
*/ | ||
async deleteAccount() { | ||
await this.enforceAuth(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the other one (line78), thanks.