Skip to content

Commit

Permalink
refactors on local.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed May 24, 2019
1 parent 230ff6f commit 270f138
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
4 changes: 4 additions & 0 deletions lib/core/utilities.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import dotProp from 'dotprop'

export const getProp = (obj, propName) => propName ? dotProp(obj, propName) : obj

export const isUnset = o => typeof o === 'undefined' || o === null
export const isSet = o => !isUnset(o)

Expand Down
42 changes: 17 additions & 25 deletions lib/schemes/local.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getProp from 'dotprop'
import { getProp } from '../utilities'

export default class LocalScheme {
constructor (auth, options) {
Expand Down Expand Up @@ -41,56 +41,52 @@ export default class LocalScheme {
// Ditch any leftover local tokens before attempting to log in
await this._logoutLocally()

// Make login request
const loginResult = await this.$auth.request(endpoint, this.options.endpoints.login)

// RememberMe
if (remember !== undefined) {
this.$auth.setRemember(this.name, remember)
}

const result = await this.$auth.request(
endpoint,
this.options.endpoints.login
)

const cookieOptions = { expires: null }
const rememberFor = this.$auth.options.cookie.options.expires

if (this.$auth.getRemember(this.name)) {
cookieOptions.expires = rememberFor
cookieOptions.expires = this.$auth.options.cookie.options.expires
}

// Update Token
if (this.options.tokenRequired) {
let token = getProp(result, this.options.endpoints.login.propertyName)
let token = getProp(loginResult, this.options.endpoints.login.propertyName)
if (this.options.tokenType) {
token = this.options.tokenType + ' ' + token
}

this.$auth.setToken(this.name, token, cookieOptions)
this._setToken(token)
}

const clientId = getProp(result, this.options.endpoints.login.clientId)

// Update client id
if (clientId !== undefined) {
// Update clientId
const clientId = getProp(loginResult, this.options.endpoints.login.clientId)
if (clientId) {
this.$auth.setClientId(this.name, clientId, cookieOptions)
}

// Fetch user
return this.fetchUser()
}

async setUserToken (tokenValue) {
async setUserToken (token) {
// Ditch any leftover local tokens before attempting to log in
await this._logoutLocally()

// Update token
if (this.options.tokenRequired) {
let token = getProp(tokenValue, this.options.endpoints.login.propertyName)
if (this.options.tokenType) {
token = this.options.tokenType + ' ' + token
}

this.$auth.setToken(this.name, token)
this._setToken(token)
}

// Fetch user
return this.fetchUser()
}

Expand All @@ -100,19 +96,15 @@ export default class LocalScheme {
return
}

// User endpoint is disabled.
// User endpoint is disabled
if (!this.options.endpoints.user) {
this.$auth.setUser({})
return
}

// Try to fetch user and then set
let user = await this.$auth.requestWith(this.name, endpoint, this.options.endpoints.user)

// Only set user if property name is defined
if (this.options.endpoints.user.propertyName) {
user = getProp(user, this.options.endpoints.user.propertyName)
}
user = getProp(user, this.options.endpoints.user.propertyName)

this.$auth.setUser(user)
}
Expand Down

0 comments on commit 270f138

Please sign in to comment.