builder login

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

View File

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

View File

@ -1,15 +1,35 @@
<script> <script>
import { Button, Label, Input, TextArea, Spacer } from "@budibase/bbui" import { Button, Label, Input, TextArea, Spacer } from "@budibase/bbui"
import { notifier } from "builderStore/store/notifications"
import { auth } from "stores/backend" import { auth } from "stores/backend"
let username = "" let username = ""
let password = "" let password = ""
async function login() { async function login() {
try {
const json = await auth.login({ const json = await auth.login({
username, username,
password, 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> </script>
@ -22,6 +42,7 @@
<Input outline type="password" on:change bind:value={password} /> <Input outline type="password" on:change bind:value={password} />
<Spacer large /> <Spacer large />
<Button primary on:click={login}>Login</Button> <Button primary on:click={login}>Login</Button>
<Button secondary on:click={createTestUser}>Create Test User</Button>
</form> </form>
<style> <style>

View File

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

View File

@ -2,19 +2,29 @@ import { writable, get } from "svelte/store"
import api from "../../builderStore/api" import api from "../../builderStore/api"
export function createAuthStore() { export function createAuthStore() {
const { subscribe } = writable({}) const { subscribe, set } = writable({})
const user = localStorage.getItem("auth:user")
if (user) set({ user: JSON.parse(user) })
return { return {
subscribe, subscribe,
login: async () => { login: async creds => {
const response = await api.post(`/api/admin/auth/authenticate`) const response = await api.post(`/api/admin/auth`, creds)
const json = await response.json() 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 () => { logout: async () => {
const response = await api.post(`/api/auth/logout`) const response = await api.post(`/api/auth/logout`)
const json = await response.json() 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() const expires = new Date()
expires.setDate(expires.getDate() + 1) expires.setDate(expires.getDate() + 1)
if (!user) {
ctx.body = { success: false, user }
return
}
ctx.cookies.set(Cookies.Auth, user.token, { ctx.cookies.set(Cookies.Auth, user.token, {
expires, expires,
path: "/", path: "/",
@ -16,7 +21,9 @@ exports.authenticate = async (ctx, next) => {
overwrite: true, overwrite: true,
}) })
ctx.body = { success: true } delete user.token
ctx.body = { success: true, user }
})(ctx, next) })(ctx, next)
} }