-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathfake-backend.js
58 lines (50 loc) · 2.06 KB
/
fake-backend.js
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
/* global window */
export function configureFakeBackend () {
let users = [{ id: 1, username: 'test', password: 'test', firstName: 'Test', lastName: 'User' }]
let realFetch = window.fetch
window.fetch = function (url, opts) {
return new Promise((resolve, reject) => {
// wrap in timeout to simulate server api call
setTimeout(() => {
// authenticate
if (url.endsWith('/users/authenticate') && opts.method === 'POST') {
// get parameters from post request
let params = JSON.parse(opts.body)
// find if any user matches login credentials
let filteredUsers = users.filter((user) => {
return user.username === params.username && user.password === params.password
})
if (filteredUsers.length) {
// if login details are valid return user details and fake jwt token
let user = filteredUsers[0]
let responseJson = {
id: user.id,
username: user.username,
firstName: user.firstName,
lastName: user.lastName,
token: 'fake-jwt-token',
}
resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(responseJson)) })
} else {
// else return error
reject('Username or password is incorrect')
}
return
}
// get users
if (url.endsWith('/users') && opts.method === 'GET') {
// check for fake auth token in header and return users if valid, this security is implemented server side in a real application
if (opts.headers && opts.headers.Authorization === 'Bearer fake-jwt-token') {
resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(users)) })
} else {
// return 401 not authorised if token is null or invalid
reject('Unauthorised')
}
return
}
// pass through any requests not handled above
realFetch(url, opts).then((response) => resolve(response))
}, 500)
})
}
}