builder login

This commit is contained in:
Martin McKeaveney 2021-04-12 10:47:48 +01:00
parent b5a5df7f60
commit d7e187d8f2
5 changed files with 49 additions and 12 deletions

View File

@ -26,7 +26,7 @@ static_resources:
cluster: redis-service
prefix_rewrite: "/"
- match: { prefix: "/api/admin" }
- match: { prefix: "/api/admin/" }
route:
cluster: worker-dev

View File

@ -1,15 +1,35 @@
<script>
import { Button, Label, Input, TextArea, Spacer } from "@budibase/bbui"
import { notifier } from "builderStore/store/notifications"
import { auth } from "stores/backend"
let username = ""
let password = ""
async function login() {
const json = await auth.login({
username,
password,
})
try {
const json = await auth.login({
username,
password,
})
notifier.success("Logged in successfully.")
} catch (err) {
console.error(err)
notifier.danger(`Error logging in: ${err}`)
}
}
async function createTestUser() {
try {
const json = await auth.createUser({
email: "test@test.com",
password: "test",
roles: {},
})
notifier.success("Test user created")
} catch (err) {
console.error(err)
}
}
</script>
@ -22,6 +42,7 @@
<Input outline type="password" on:change bind:value={password} />
<Spacer large />
<Button primary on:click={login}>Login</Button>
<Button secondary on:click={createTestUser}>Create Test User</Button>
</form>
<style>

View File

@ -50,7 +50,6 @@
<slot />
</div>
</div>
{:else}
<section class="login">
<LoginForm />

View File

@ -2,19 +2,29 @@ import { writable, get } from "svelte/store"
import api from "../../builderStore/api"
export function createAuthStore() {
const { subscribe } = writable({})
const { subscribe, set } = writable({})
const user = localStorage.getItem("auth:user")
if (user) set({ user: JSON.parse(user) })
return {
subscribe,
login: async () => {
const response = await api.post(`/api/admin/auth/authenticate`)
login: async creds => {
const response = await api.post(`/api/admin/auth`, creds)
const json = await response.json()
set({ user: json })
if (json.user) {
localStorage.setItem("auth:user", JSON.stringify(json.user))
set({ user: json.user })
}
},
logout: async () => {
const response = await api.post(`/api/auth/logout`)
const json = await response.json()
set({ user: null })
set({ user: false })
},
createUser: async user => {
const response = await api.post(`/api/admin/users`, user)
const json = await response.json()
},
}
}

View File

@ -9,6 +9,11 @@ exports.authenticate = async (ctx, next) => {
const expires = new Date()
expires.setDate(expires.getDate() + 1)
if (!user) {
ctx.body = { success: false, user }
return
}
ctx.cookies.set(Cookies.Auth, user.token, {
expires,
path: "/",
@ -16,7 +21,9 @@ exports.authenticate = async (ctx, next) => {
overwrite: true,
})
ctx.body = { success: true }
delete user.token
ctx.body = { success: true, user }
})(ctx, next)
}