Skip to content

Commit

Permalink
Adds user_id prop handling which was accidentally removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bakatz committed Sep 4, 2023
1 parent 5dc5675 commit b973b3e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ test('authWithURLCode() updates localStorage with the user details when the http
const simpleOTP = new SimpleOTP('mocksiteid')
const mockPost = vi.spyOn(http, 'post')
mockPost.mockImplementation(() => {
return { data: { data: { email: '[email protected]', token: 'reallysecuretoken' } } }
return { data: { data: { id: 'someid', email: '[email protected]', token: 'reallysecuretoken' } } }
})

window.location = { search: '?simpleotp_code=reallysecurecode'}
Expand All @@ -102,7 +102,7 @@ test('authWithURLCode() throws when the code is missing from the url params', as
const mockPost = vi.spyOn(http, 'post')
simpleOTP.signOut()
mockPost.mockImplementation(() => {
return { data: { data: { email: '[email protected]', token: 'reallysecuretoken' } } }
return { data: { data: { id: 'someid', email: '[email protected]', token: 'reallysecuretoken' } } }
})

window.location = { search: '?not_a_simpleotp_code=reallysecurecode'}
Expand All @@ -120,7 +120,7 @@ test('authWithURLCode() returns the error code in the response when the http cal
data: {
code: 'invalid_auth_code',
message: 'bad auth code',
data: { email: '[email protected]', token: 'reallysecuretoken' }
data: { id: 'someid', email: '[email protected]', token: 'reallysecuretoken' }
}
}
}
Expand All @@ -145,7 +145,7 @@ test('authWithURLCode() returns a networking error in the response when the http
data: {
code: 'invalid_auth_code',
message: 'bad auth code',
data: { email: '[email protected]', token: 'reallysecuretoken' }
data: { id: 'someid', email: '[email protected]', token: 'reallysecuretoken' }
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export const AuthStatusCode = Object.freeze({

export class AuthenticatedUser {
/**
*
* @param {string} id
* @param {string} email
* @param {string} token
*/
constructor (email, token) {
constructor (id, email, token) {
this.id = id
this.email = email
this.token = token
}
Expand Down Expand Up @@ -64,7 +65,7 @@ export class SimpleOTP {
if (!siteID || typeof(siteID) !== 'string') {
throw Error('siteID must be a non-empty string')
}

if (apiURL && !isValidURL(apiURL)) {
throw Error('apiURL must be a valid URL if defined')
}
Expand Down Expand Up @@ -128,7 +129,7 @@ export class SimpleOTP {

const httpResponseData = response.data
const apiResponseData = httpResponseData.data
const user = new AuthenticatedUser(apiResponseData.email, apiResponseData.token)
const user = new AuthenticatedUser(apiResponseData.id, apiResponseData.email, apiResponseData.token)
localStorage.setItem(this.simpleOTPUserKey, JSON.stringify(user))
return new SiteAuthResponse(httpResponseData.code, httpResponseData.message, httpResponseData.data)
}
Expand All @@ -144,7 +145,7 @@ export class SimpleOTP {
return null
}
const userObj = JSON.parse(user)
return new AuthenticatedUser(userObj.email, userObj.token)
return new AuthenticatedUser(userObj.id, userObj.email, userObj.token)
}

/**
Expand All @@ -161,4 +162,4 @@ export class SimpleOTP {
signOut() {
localStorage.removeItem(this.simpleOTPUserKey)
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@simpleotp/core",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit b973b3e

Please sign in to comment.