· 8 years ago · Dec 28, 2016, 06:34 PM
1import ApiClient from './ApiClient'
2import { Cookies } from 'quasar-framework'
3
4export default class AuthService extends ApiClient {
5 login({username, password}) {
6 const payload = {
7 client_id: this.clientId,
8 client_secret: this.clientSecret,
9 grant_type: 'password',
10 scope: '*',
11 username: username,
12 password: password
13 }
14
15 return this.api.post(this.baseUrl + '/oauth/token', payload).then(response => {
16 /**
17 * If the response is ok, we set the new access_token cookie
18 * and make sure we also update the authorisation header
19 */
20 if(response.ok) {
21 const access_token = response.body.access_token
22 Cookies.set('accessToken', access_token, {path: '/'})
23 Cookies.set('isAuthenticated', true, {path: '/'})
24 this.setAuthorisationHeader(access_token)
25 }
26
27 return response
28 });
29 }
30
31 getAuthUser() {
32 return this.api.get(this.url('user'))
33 }
34}