2021-05-22 02:23:39 +12:00
|
|
|
import { derived, writable, get } from "svelte/store"
|
2021-04-11 22:35:55 +12:00
|
|
|
import api from "../../builderStore/api"
|
|
|
|
|
|
|
|
export function createAuthStore() {
|
2021-05-22 00:17:09 +12:00
|
|
|
const user = writable(null)
|
|
|
|
const store = derived(user, $user => {
|
|
|
|
let initials = null
|
|
|
|
if ($user) {
|
|
|
|
if ($user.firstName) {
|
|
|
|
initials = $user.firstName[0]
|
|
|
|
if ($user.lastName) {
|
|
|
|
initials += $user.lastName[0]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
initials = $user.email[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
user: $user,
|
|
|
|
initials,
|
|
|
|
}
|
|
|
|
})
|
2021-04-11 22:35:55 +12:00
|
|
|
|
|
|
|
return {
|
2021-05-07 01:49:11 +12:00
|
|
|
subscribe: store.subscribe,
|
|
|
|
checkAuth: async () => {
|
2021-05-20 01:05:08 +12:00
|
|
|
const response = await api.get("/api/admin/users/self")
|
2021-05-20 23:39:33 +12:00
|
|
|
if (response.status !== 200) {
|
2021-05-22 00:17:09 +12:00
|
|
|
user.set(null)
|
2021-05-20 23:39:33 +12:00
|
|
|
} else {
|
2021-05-22 00:17:09 +12:00
|
|
|
const json = await response.json()
|
|
|
|
user.set(json)
|
2021-05-07 01:49:11 +12:00
|
|
|
}
|
|
|
|
},
|
2021-05-04 22:32:22 +12:00
|
|
|
login: async creds => {
|
2021-04-12 21:47:48 +12:00
|
|
|
const response = await api.post(`/api/admin/auth`, creds)
|
2021-04-11 22:35:55 +12:00
|
|
|
const json = await response.json()
|
2021-04-16 02:57:55 +12:00
|
|
|
if (response.status === 200) {
|
2021-05-22 00:17:09 +12:00
|
|
|
user.set(json.user)
|
2021-04-16 02:57:55 +12:00
|
|
|
} else {
|
|
|
|
throw "Invalid credentials"
|
|
|
|
}
|
2021-04-13 22:56:57 +12:00
|
|
|
return json
|
2021-04-11 22:35:55 +12:00
|
|
|
},
|
|
|
|
logout: async () => {
|
2021-04-14 00:56:28 +12:00
|
|
|
const response = await api.post(`/api/admin/auth/logout`)
|
2021-04-21 04:17:44 +12:00
|
|
|
if (response.status !== 200) {
|
|
|
|
throw "Unable to create logout"
|
|
|
|
}
|
2021-04-15 02:43:34 +12:00
|
|
|
await response.json()
|
2021-05-22 00:17:09 +12:00
|
|
|
user.set(null)
|
2021-04-12 21:47:48 +12:00
|
|
|
},
|
2021-05-22 02:23:39 +12:00
|
|
|
updateSelf: async fields => {
|
|
|
|
const newUser = { ...get(user), ...fields }
|
2021-05-22 00:17:09 +12:00
|
|
|
const response = await api.post("/api/admin/users/self", newUser)
|
2021-05-20 01:05:08 +12:00
|
|
|
if (response.status === 200) {
|
2021-05-22 02:23:39 +12:00
|
|
|
user.set(newUser)
|
2021-05-20 01:05:08 +12:00
|
|
|
} else {
|
|
|
|
throw "Unable to update user details"
|
|
|
|
}
|
|
|
|
},
|
2021-05-19 02:26:09 +12:00
|
|
|
forgotPassword: async email => {
|
|
|
|
const response = await api.post(`/api/admin/auth/reset`, {
|
|
|
|
email,
|
|
|
|
})
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw "Unable to send email with reset link"
|
|
|
|
}
|
|
|
|
await response.json()
|
|
|
|
},
|
|
|
|
resetPassword: async (password, code) => {
|
|
|
|
const response = await api.post(`/api/admin/auth/reset/update`, {
|
|
|
|
password,
|
|
|
|
resetCode: code,
|
|
|
|
})
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw "Unable to reset password"
|
|
|
|
}
|
|
|
|
await response.json()
|
|
|
|
},
|
2021-05-04 22:32:22 +12:00
|
|
|
createUser: async user => {
|
2021-04-12 21:47:48 +12:00
|
|
|
const response = await api.post(`/api/admin/users`, user)
|
2021-04-21 04:17:44 +12:00
|
|
|
if (response.status !== 200) {
|
|
|
|
throw "Unable to create user"
|
|
|
|
}
|
|
|
|
await response.json()
|
|
|
|
},
|
2021-04-11 22:35:55 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const auth = createAuthStore()
|