-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapi.js
More file actions
139 lines (127 loc) · 3.77 KB
/
api.js
File metadata and controls
139 lines (127 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import feathers from '@feathersjs/feathers'
import socketio from '@feathersjs/socketio-client'
import io from 'socket.io-client'
import authentication from '@feathersjs/authentication-client'
import urlHelper from '~/helpers/urls'
import Vue from 'vue'
export default ({app, env, store, redirect, router}) => {
const authKey = 'feathers-jwt'
const endpoint = urlHelper.buildEndpointURL(env.API_HOST, { port: env.API_PORT })
const storage = {
getItem: (key) => {
const res = app.$cookies.get(key)
// console.log(`## STORAGE: getItem(${key})`, res)
return res
},
setItem: (key, value, options) => {
const res = app.$cookies.set(key, value, options)
// console.log(`## STORAGE: setItem(${key}, ${value}, ${options})`, res)
return res
},
removeItem: (key) => {
const res = app.$cookies.remove(key)
// console.log(`## STORAGE: removeItem(${key})`, res)
return res
},
clear: () => {
const res = app.$cookies.removeAll()
if (env.NODE_ENV === 'development') {
console.log(`## STORAGE: clear()`, res)
}
return res
}
}
const socket = io(endpoint)
if (process.server) {
setTimeout(() => {
// close server connection as content was delivered already after 30 seconds at latest
try {
socket.close()
} catch (err) {
app.error(err)
}
}, 30000)
}
let api = feathers()
.configure(socketio(socket, {
timeout: 20000
}))
.configure(authentication({
storage: storage,
storageKey: authKey,
cookie: authKey
}))
api.hooks({
before: {
all: [
async (hook) => {
// hook.accessToken = await api.passport.getJWT()
if (env.NODE_ENV === 'development') {
console.log('# API:', `${hook.method} ${hook.path}`)
console.info('data', hook.data)
// console.log('# ' + hook.accessToken)
}
return hook
}
]
},
async error (ctx) {
if (env.NODE_ENV === 'development') {
console.log('####################')
console.error(ctx.error)
console.info('JWT TOKEN: ', app.$cookies.get(authKey))
console.info('path', ctx.path)
console.info('service', ctx.service)
console.info('method', ctx.method)
console.info('params', ctx.params)
console.info('id', ctx.id)
console.info('data', ctx.data)
console.log('####################')
}
// force re-login on 401 responses
if (process.client && ctx.error.code === 401) {
await store.dispatch('auth/logout')
redirect(`/auth/login?path=${window.location.pathname}`)
}
}
})
/**
* (Re-)Authenticate a user by credentials or jwt token
*
* returns the user object or null
* @param {Object} options
*/
api.auth = async (options = {strategy: 'jwt'}) => {
// console.log('~~~######### api.auth', options)
if (options.strategy === 'local') {
// fix issues where we could not log in
// when at development
await api.passport.logout()
storage.removeItem(authKey)
}
let user = null
const response = await api.authenticate(options)
const payload = await api.passport.verifyJWT(response.accessToken)
if (payload.userId) {
user = await api.service('users').get(payload.userId)
}
return user
}
api.on('connection', connection => {
// On a new real-time connection, add it to the anonymous channel
api.channel('authenticated').join(connection)
})
/**
* @deprecated
*/
api.authKey = authKey
// make the api accessible inside vue components
Vue.use({
install (Vue) {
Vue.prototype.$api = api
}
})
// make the api accessible through app.$api
app.$api = api
return api
}