-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pooya Parsa
committed
Feb 22, 2018
1 parent
71589d0
commit fdb98d2
Showing
4 changed files
with
114 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,81 @@ | ||
const DEFAULTS = { | ||
tokenRequired: true, | ||
tokenType: 'Bearer', | ||
globalToken: true | ||
} | ||
|
||
export default class LocalScheme { | ||
constructor (auth, options) { | ||
this.auth = auth | ||
this.options = Object.assign( | ||
{ tokenRequired: true, tokenType: 'Bearer' }, | ||
options | ||
) | ||
this.name = options._name | ||
|
||
this.options = Object.assign({}, DEFAULTS, options) | ||
} | ||
|
||
_setToken (token) { | ||
// Set Authorization token for all axios requests | ||
this.auth.ctx.app.$axios.setToken(token, this.options.tokenType) | ||
if (this.options.globalToken) { | ||
// Set Authorization token for all axios requests | ||
this.auth.ctx.app.$axios.setToken(token, this.options.tokenType) | ||
} | ||
} | ||
|
||
mounted () { | ||
if (this.options.tokenRequired) { | ||
const token = this.auth.syncToken() | ||
const token = this.auth.syncToken(this.name) | ||
this._setToken(token) | ||
} | ||
} | ||
|
||
login (endpoint) { | ||
async login (endpoint) { | ||
if (!this.options.endpoints.login) { | ||
return Promise.resolve() | ||
return | ||
} | ||
|
||
return this.auth | ||
.request(endpoint, this.options.endpoints.login) | ||
.then(token => { | ||
if (this.options.tokenRequired) { | ||
this.auth.setToken(token) | ||
this._setToken(token) | ||
} | ||
}) | ||
.then(() => this.fetchUser()) | ||
const result = await this.auth.request( | ||
endpoint, | ||
this.options.endpoints.login | ||
) | ||
|
||
if (this.options.tokenRequired) { | ||
const token = this.options.tokenType | ||
? this.options.tokenType + ' ' + result | ||
: result | ||
|
||
this.auth.setToken(this.name, token) | ||
this._setToken(token) | ||
} | ||
|
||
return this.fetchUser() | ||
} | ||
|
||
fetchUser (endpoint) { | ||
async fetchUser (endpoint) { | ||
// User endpoint is disabled. | ||
if (!this.options.endpoints.user) { | ||
this.auth.setUser({}) | ||
return Promise.resolve() | ||
return | ||
} | ||
|
||
// Token is required but not available | ||
if (this.options.tokenRequired && !this.auth.getToken()) { | ||
return Promise.resolve() | ||
if (this.options.tokenRequired && !this.auth.getToken(this.name)) { | ||
return | ||
} | ||
|
||
// Try to fetch user and then set loggedIn to true | ||
return this.auth | ||
.request(endpoint, this.options.endpoints.user) | ||
.then(user => this.auth.setUser(user)) | ||
// Try to fetch user and then set | ||
const user = await this.auth.requestWith( | ||
this.name, | ||
endpoint, | ||
this.options.endpoints.user | ||
) | ||
this.auth.setUser(user) | ||
} | ||
|
||
logout (endpoint) { | ||
if (!this.options.endpoints.logout) { | ||
return Promise.resolve() | ||
return | ||
} | ||
|
||
return this.auth | ||
.request(endpoint, this.options.endpoints.logout) | ||
.requestWith(this.name, endpoint, this.options.endpoints.logout) | ||
.catch(() => {}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters