Some changes to initial login form, improvements based on testing and attempts to fix cypress test failures.

This commit is contained in:
mike12345567 2021-04-15 15:57:55 +01:00
parent 0d65aac9a4
commit cfbd75b36d
9 changed files with 79 additions and 24 deletions

View File

@ -1,5 +1,5 @@
<script> <script>
import { Button, Label, Input, TextArea, Spacer } from "@budibase/bbui" import { Button, Label, Input, Spacer } from "@budibase/bbui"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { auth } from "stores/backend" import { auth } from "stores/backend"
@ -8,24 +8,20 @@
async function login() { async function login() {
try { try {
const json = await auth.login({ await auth.login({
username, username,
password, password,
}) })
if (json.success) { notifier.success("Logged in successfully.")
notifier.success("Logged in successfully.")
} else {
notifier.danger("Invalid credentials")
}
} catch (err) { } catch (err) {
console.error(err) console.error(err)
notifier.danger(`Error logging in: ${err}`) notifier.danger("Invalid credentials")
} }
} }
async function createTestUser() { async function createTestUser() {
try { try {
const json = await auth.createUser({ await auth.createUser({
email: "test@test.com", email: "test@test.com",
password: "test", password: "test",
roles: {}, roles: {},
@ -36,6 +32,7 @@
notifier.success("Test user created") notifier.success("Test user created")
} catch (err) { } catch (err) {
console.error(err) console.error(err)
notifier.danger("Could not create test user")
} }
} }
</script> </script>

View File

@ -1,5 +1,5 @@
<script> <script>
import { Home as Link, Button } from "@budibase/bbui" import { Home as Link } from "@budibase/bbui"
import { import {
AppsIcon, AppsIcon,
HostingIcon, HostingIcon,

View File

@ -21,7 +21,11 @@ export function createAuthStore() {
login: async creds => { login: async creds => {
const response = await api.post(`/api/admin/auth`, creds) const response = await api.post(`/api/admin/auth`, creds)
const json = await response.json() const json = await response.json()
set({ user: json }) if (response.status === 200) {
set({ user: json.user })
} else {
throw "Invalid credentials"
}
return json return json
}, },
logout: async () => { logout: async () => {

View File

@ -0,0 +1,34 @@
const fetch = require("node-fetch")
const env = require("../../environment")
const { checkSlashesInUrl } = require("../../utilities")
const { request } = require("../../utilities/workerRequests")
async function redirect(ctx, method) {
const { path } = ctx.params
const response = await fetch(
checkSlashesInUrl(`${env.WORKER_URL}/api/admin/${path}`),
request(ctx, {
method,
body: ctx.request.body,
})
)
ctx.body = await response.json()
const cookie = response.headers.get("set-cookie")
if (cookie) {
ctx.set("set-cookie", cookie)
}
ctx.status = response.status
ctx.cookies
}
exports.redirectGet = async ctx => {
await redirect(ctx, "GET")
}
exports.redirectPost = async ctx => {
await redirect(ctx, "POST")
}
exports.redirectDelete = async ctx => {
await redirect(ctx, "DELETE")
}

View File

@ -0,0 +1,13 @@
const Router = require("@koa/router")
const controller = require("../controllers/dev")
const env = require("../../environment")
const router = Router()
if (env.isDev()) {
router.get("/api/admin/:path", controller.redirectGet)
router.post("/api/admin/:path", controller.redirectPost)
router.delete("/api/admin/:path", controller.redirectDelete)
}
module.exports = router

View File

@ -22,6 +22,7 @@ const datasourceRoutes = require("./datasource")
const queryRoutes = require("./query") const queryRoutes = require("./query")
const hostingRoutes = require("./hosting") const hostingRoutes = require("./hosting")
const backupRoutes = require("./backup") const backupRoutes = require("./backup")
const devRoutes = require("./dev")
exports.mainRoutes = [ exports.mainRoutes = [
deployRoutes, deployRoutes,
@ -44,6 +45,7 @@ exports.mainRoutes = [
queryRoutes, queryRoutes,
hostingRoutes, hostingRoutes,
backupRoutes, backupRoutes,
devRoutes,
// these need to be handled last as they still use /api/:tableId // these need to be handled last as they still use /api/:tableId
// this could be breaking as koa may recognise other routes as this // this could be breaking as koa may recognise other routes as this
tableRoutes, tableRoutes,

View File

@ -9,10 +9,11 @@ exports.getFullUser = async ({ ctx, email, userId }) => {
if (!email) { if (!email) {
email = getEmailFromUserMetadataID(userId) email = getEmailFromUserMetadataID(userId)
} }
const db = new CouchDB(ctx.appId)
const global = await getGlobalUsers(ctx, ctx.appId, email) const global = await getGlobalUsers(ctx, ctx.appId, email)
let metadata let metadata
try { try {
// this will throw an error if the db doesn't exist, or there is no appId
const db = new CouchDB(ctx.appId)
metadata = await db.get(generateUserMetadataID(email)) metadata = await db.get(generateUserMetadataID(email))
} catch (err) { } catch (err) {
// it is fine if there is no user metadata, just remove global db info // it is fine if there is no user metadata, just remove global db info

View File

@ -11,11 +11,11 @@ function getAppRole(appId, user) {
if (!user.roleId) { if (!user.roleId) {
user.roleId = BUILTIN_ROLE_IDS.PUBLIC user.roleId = BUILTIN_ROLE_IDS.PUBLIC
} }
// delete user.roles delete user.roles
return user return user
} }
function prepRequest(ctx, request) { function request(ctx, request) {
if (!request.headers) { if (!request.headers) {
request.headers = {} request.headers = {}
} }
@ -32,6 +32,8 @@ function prepRequest(ctx, request) {
return request return request
} }
exports.request = request
exports.getDeployedApps = async ctx => { exports.getDeployedApps = async ctx => {
if (!env.SELF_HOSTED) { if (!env.SELF_HOSTED) {
throw "Can only check apps for self hosted environments" throw "Can only check apps for self hosted environments"
@ -39,7 +41,7 @@ exports.getDeployedApps = async ctx => {
try { try {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/apps`), checkSlashesInUrl(env.WORKER_URL + `/api/apps`),
prepRequest(ctx, { request(ctx, {
method: "GET", method: "GET",
}) })
) )
@ -63,19 +65,22 @@ exports.deleteGlobalUser = async (ctx, email) => {
const reqCfg = { method: "DELETE" } const reqCfg = { method: "DELETE" }
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint), checkSlashesInUrl(env.WORKER_URL + endpoint),
prepRequest(ctx, reqCfg) request(ctx, reqCfg)
) )
return response.json() return response.json()
} }
exports.getGlobalUsers = async (ctx, appId, email = null) => { exports.getGlobalUsers = async (ctx, appId = null, email = null) => {
const endpoint = email ? `/api/admin/users/${email}` : `/api/admin/users` const endpoint = email ? `/api/admin/users/${email}` : `/api/admin/users`
const reqCfg = { method: "GET" } const reqCfg = { method: "GET" }
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint), checkSlashesInUrl(env.WORKER_URL + endpoint),
prepRequest(ctx, reqCfg) request(ctx, reqCfg)
) )
let users = await response.json() let users = await response.json()
if (!appId) {
return users
}
if (Array.isArray(users)) { if (Array.isArray(users)) {
users = users.map(user => getAppRole(appId, user)) users = users.map(user => getAppRole(appId, user))
} else { } else {
@ -107,7 +112,7 @@ exports.saveGlobalUser = async (ctx, appId, email, body) => {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + endpoint), checkSlashesInUrl(env.WORKER_URL + endpoint),
prepRequest(ctx, reqCfg) request(ctx, reqCfg)
) )
const json = await response.json() const json = await response.json()
if (json.status !== 200 && response.status !== 200) { if (json.status !== 200 && response.status !== 200) {

View File

@ -3,15 +3,14 @@ const { passport, Cookies, clearCookie } = require("@budibase/auth")
exports.authenticate = async (ctx, next) => { exports.authenticate = async (ctx, next) => {
return passport.authenticate("local", async (err, user) => { return passport.authenticate("local", async (err, user) => {
if (err) { if (err) {
return ctx.throw(err) return ctx.throw(403, "Unauthorized")
} }
const expires = new Date() const expires = new Date()
expires.setDate(expires.getDate() + 1) expires.setDate(expires.getDate() + 1)
if (!user) { if (!user) {
ctx.body = { success: false } return ctx.throw(403, "Unauthorized")
return next()
} }
ctx.cookies.set(Cookies.Auth, user.token, { ctx.cookies.set(Cookies.Auth, user.token, {
@ -23,13 +22,13 @@ exports.authenticate = async (ctx, next) => {
delete user.token delete user.token
ctx.body = { success: true, user } ctx.body = { user }
})(ctx, next) })(ctx, next)
} }
exports.logout = async ctx => { exports.logout = async ctx => {
clearCookie(ctx, Cookies.Auth) clearCookie(ctx, Cookies.Auth)
ctx.body = { success: true } ctx.body = { messaged: "User logged out" }
} }
exports.googleAuth = async () => { exports.googleAuth = async () => {