|
| 1 | +import Vue from 'vue' |
| 2 | +import Hookable from 'hookable' |
1 | 3 | import { isArray, isObject } from 'lodash'
|
2 | 4 |
|
3 |
| -class Strapi { |
4 |
| - constructor ($axios, $cookies) { |
5 |
| - this.$axios = $axios |
6 |
| - this.$cookies = $cookies |
| 5 | +const TOKEN_KEY = 'strapi_jwt' |
| 6 | + |
| 7 | +class Strapi extends Hookable { |
| 8 | + constructor (ctx) { |
| 9 | + super() |
| 10 | + |
| 11 | + this.state = Vue.observable({ user: null }) |
| 12 | + |
| 13 | + this.$cookies = ctx.app.$cookies |
| 14 | + this.$http = ctx.$http.create({}) |
| 15 | + this.$http.setBaseURL('<%= options.url %>') |
| 16 | + this.$http.onError(async (err) => { |
| 17 | + const { status } = err.response |
| 18 | + const { message: msg } = await err.response.json() |
| 19 | + |
| 20 | + let message |
| 21 | + if (isArray(msg)) { |
| 22 | + message = msg[0].messages[0].message |
| 23 | + } else if (isObject(msg)) { |
| 24 | + message = msg.message |
| 25 | + } else { |
| 26 | + message = msg |
| 27 | + } |
| 28 | + |
| 29 | + err.message = message |
| 30 | + err.statusCode = status |
| 31 | + this.callHook('error', err) |
| 32 | + }) |
7 | 33 | }
|
8 | 34 |
|
9 |
| - async request (method, url, config) { |
10 |
| - const { data } = await this.$axios({ |
11 |
| - method, |
12 |
| - url, |
13 |
| - ...config |
14 |
| - }) |
| 35 | + get user () { |
| 36 | + return this.state.user |
| 37 | + } |
15 | 38 |
|
16 |
| - return data |
| 39 | + set user (user) { |
| 40 | + Vue.set(this.state, 'user', user) |
| 41 | + } |
| 42 | + |
| 43 | + async register (data) { |
| 44 | + this.clearToken() |
| 45 | + const { user, jwt } = await this.$http.$post('/auth/local/register', data) |
| 46 | + this.setToken(jwt) |
| 47 | + this.setUser(user) |
| 48 | + return { user, jwt } |
17 | 49 | }
|
18 | 50 |
|
19 |
| - async register (email, password) { |
| 51 | + async login (data) { |
20 | 52 | this.clearToken()
|
21 |
| - const authentication = await this.$axios.$post('/auth/local/register', { email, password }) |
22 |
| - this.setToken(authentication.jwt) |
23 |
| - return authentication |
| 53 | + const { user, jwt } = await this.$http.$post('/auth/local', data) |
| 54 | + this.setToken(jwt) |
| 55 | + this.setUser(user) |
| 56 | + return { user, jwt } |
24 | 57 | }
|
25 | 58 |
|
26 |
| - async login (email, password) { |
| 59 | + forgotPassword (data) { |
27 | 60 | this.clearToken()
|
28 |
| - const authentication = await this.$axios.$post('/auth/local', { email, password }) |
29 |
| - this.setToken(authentication.jwt) |
30 |
| - return authentication |
| 61 | + return this.$http.$post('/auth/forgot-password', data) |
31 | 62 | }
|
32 | 63 |
|
33 |
| - forgotPassword (email, url) { |
| 64 | + async resetPassword (data) { |
34 | 65 | this.clearToken()
|
35 |
| - return this.$axios.$post('/auth/forgot-password', { email, url }) |
| 66 | + const { user, jwt } = await this.$http.$post('/auth/reset-password', data) |
| 67 | + this.setToken(jwt) |
| 68 | + this.setUser(user) |
| 69 | + return { user, jwt } |
36 | 70 | }
|
37 | 71 |
|
38 |
| - async resetPassword (code, password, passwordConfirmation) { |
| 72 | + logout () { |
| 73 | + this.setUser(null) |
39 | 74 | this.clearToken()
|
40 |
| - const authentication = await this.$axios.$post('/auth/reset-password', { code, password, passwordConfirmation }) |
41 |
| - this.setToken(authentication.jwt) |
42 |
| - return authentication |
| 75 | + } |
| 76 | + |
| 77 | + async fetchUser () { |
| 78 | + const jwt = this.getToken() |
| 79 | + if (!jwt) { |
| 80 | + return null |
| 81 | + } |
| 82 | + |
| 83 | + this.$http.setToken(jwt, 'Bearer') |
| 84 | + |
| 85 | + try { |
| 86 | + const user = await this.getEntry('users', 'me') |
| 87 | + this.setUser(user) |
| 88 | + } catch (e) { |
| 89 | + this.clearToken() |
| 90 | + } |
| 91 | + |
| 92 | + return this.user |
| 93 | + } |
| 94 | + |
| 95 | + setUser (user) { |
| 96 | + this.user = user |
43 | 97 | }
|
44 | 98 |
|
45 | 99 | getEntries (contentTypePluralized, params) {
|
46 |
| - return this.$axios.$get(`/${contentTypePluralized}`, { params }) |
| 100 | + return this.$http.$get(`/${contentTypePluralized}`, { params }) |
47 | 101 | }
|
48 | 102 |
|
49 | 103 | getEntryCount (contentType, params) {
|
50 |
| - return this.$axios.$get(`/${contentType}/count`, { params }) |
| 104 | + return this.$http.$get(`/${contentType}/count`, { params }) |
51 | 105 | }
|
52 | 106 |
|
53 | 107 | getEntry (contentTypePluralized, id) {
|
54 |
| - return this.$axios.$get(`/${contentTypePluralized}/${id}`) |
| 108 | + return this.$http.$get(`/${contentTypePluralized}/${id}`) |
55 | 109 | }
|
56 | 110 |
|
57 | 111 | createEntry (contentTypePluralized, data) {
|
58 |
| - return this.$axios.$post(`/${contentTypePluralized}`, data) |
| 112 | + return this.$http.$post(`/${contentTypePluralized}`, data) |
59 | 113 | }
|
60 | 114 |
|
61 | 115 | updateEntry (contentTypePluralized, id, data) {
|
62 |
| - return this.$axios.$put(`/${contentTypePluralized}/${id}`, data) |
| 116 | + return this.$http.$put(`/${contentTypePluralized}/${id}`, data) |
63 | 117 | }
|
64 | 118 |
|
65 | 119 | deleteEntry (contentTypePluralized, id) {
|
66 |
| - return this.$axios.$delete(`/${contentTypePluralized}/${id}`) |
| 120 | + return this.$http.$delete(`/${contentTypePluralized}/${id}`) |
67 | 121 | }
|
68 | 122 |
|
69 |
| - setToken (jwt, comesFromStorage) { |
70 |
| - this.$axios.setToken(jwt, 'Bearer') |
71 |
| - if (!comesFromStorage) { |
72 |
| - this.$cookies.set('jwt', jwt) |
73 |
| - } |
| 123 | + getToken () { |
| 124 | + return this.$cookies.get(TOKEN_KEY) |
| 125 | + } |
| 126 | + |
| 127 | + setToken (jwt) { |
| 128 | + this.$http.setToken(jwt, 'Bearer') |
| 129 | + this.$cookies.set(TOKEN_KEY, jwt) |
74 | 130 | }
|
75 | 131 |
|
76 | 132 | clearToken () {
|
77 |
| - this.$axios.setToken(false) |
78 |
| - this.$cookies.remove('jwt') |
| 133 | + this.$http.setToken(false) |
| 134 | + this.$cookies.remove(TOKEN_KEY) |
79 | 135 | }
|
80 | 136 | }
|
81 | 137 |
|
82 |
| -export default function ({ $axios, app }, inject) { |
83 |
| - const strapi = new Strapi($axios, app.$cookies) |
| 138 | +export default async function (ctx, inject) { |
| 139 | + const strapi = new Strapi(ctx) |
84 | 140 |
|
85 |
| - $axios.onError((error) => { |
86 |
| - const { response: { data: { message } } } = error |
| 141 | + // Check if jwt to get user |
| 142 | + await strapi.fetchUser() |
87 | 143 |
|
88 |
| - let description |
89 |
| - if (isArray(message)) { |
90 |
| - description = message[0].messages[0].message |
91 |
| - } else if (isObject(message)) { |
92 |
| - description = message.message |
93 |
| - } else { |
94 |
| - description = message |
95 |
| - } |
96 |
| - |
97 |
| - if (app.$toast) { |
98 |
| - app.$toast.error(description) |
99 |
| - } else { |
100 |
| - throw new Error(description) |
101 |
| - } |
102 |
| - }) |
| 144 | + // TODO: Fetch only server-side universal |
103 | 145 |
|
104 | 146 | inject('strapi', strapi)
|
| 147 | + ctx.$strapi = strapi |
105 | 148 | }
|
0 commit comments