Skip to content

Commit

Permalink
Tried to fix a admin thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Obadja Ris committed Sep 30, 2020
1 parent f7b049c commit d7268e2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 62 deletions.
4 changes: 2 additions & 2 deletions app/auth/mutations/login.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SessionContext } from "blitz"
import { authenticateUser } from "app/auth/auth-utils"
import { LoginInput, LoginInputType } from "../validations"

export let LogIn = false
export default async function login(input: LoginInputType, ctx: { session?: SessionContext } = {}) {
// This throws an error if input is invalid
const { email, password } = LoginInput.parse(input)
Expand All @@ -10,6 +10,6 @@ export default async function login(input: LoginInputType, ctx: { session?: Sess
const user = await authenticateUser(email, password)

await ctx.session!.create({ userId: user.id, roles: [user.role] })

LogIn = true
return user
}
1 change: 0 additions & 1 deletion app/auth/mutations/logout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SessionContext } from "blitz"

export default async function logout(_ = null, ctx: { session?: SessionContext } = {}) {
return await ctx.session!.revoke()
}
1 change: 0 additions & 1 deletion app/hooks/useCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useQuery, useSession } from "blitz"
import getCurrentUser from "app/users/queries/getCurrentUser"

export const useCurrentUser = () => {
// We wouldn't have to useSession() here, but doing so improves perf on initial
// load since we can skip the getCurrentUser() request.
Expand Down
Empty file removed app/layouts/style.css
Empty file.
21 changes: 0 additions & 21 deletions app/pages/Fietsen.tsx

This file was deleted.

57 changes: 20 additions & 37 deletions app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,61 +1,38 @@
import React, { Suspense } from "react"
import Layout from "app/layouts/Layout"
import { Head, Link, usePaginatedQuery, useRouter, BlitzPage } from "blitz"
import { Head, BlitzPage } from "blitz"
import { useCurrentUser } from "app/hooks/useCurrentUser"
import logout from "app/auth/mutations/logout"
import { LabeledTextField } from "app/components/LabeledTextField"
import { Form, FORM_ERROR } from "app/components/Form"
import login from "app/auth/mutations/login"
import { LoginInput, LoginInputType } from "app/auth/validations"
const ITEMS_PER_PAGE = 100

const loginForm = () => {
return (
<div className="base-container">
<div className="header">Login</div>
<div className="content">
<div className="form">
<div className="formGroup">
<label htmlFor="username">Username</label>
<input type="text" name="username" placeholder="username" />
</div>
<div className="form">
<div className="formGroup">
<label htmlFor="password">Password</label>
<input type="password" name="password" placeholder="password" />
</div>
</div>
</div>
</div>
<div className="footer">
<button type="button" className="btn">
Login
</button>
</div>
</div>
)
}
import { CurrentUser } from "app/pages/CurrentUser"
type LoginFormProps = {
onSuccess?: () => void
onLogOut?: () => void
}
const UserInfo = (props: LoginFormProps) => {
const currentUser = useCurrentUser()

if (currentUser) {
return (
<>
<button
className="button small"
onClick={async () => {
await logout()
if (props.onLogOut) {
props.onLogOut()
}
}}
>
Logout
</button>
<div>
User id: <code>{currentUser.id}</code>
<br />
User role: <code>{currentUser.role}</code>
<p>
Hallo {currentUser.email}!, Welkom op de site! Uw UserID is:
<code>{currentUser.id}</code>. Uw UserRole is: <code>{currentUser.role}</code>
</p>
</div>
</>
)
Expand All @@ -68,7 +45,9 @@ const UserInfo = (props: LoginFormProps) => {
onSubmit={async (values) => {
try {
await login({ email: values.email, password: values.password })
props.onSuccess && props.onSuccess()
if (props.onSuccess) {
props.onSuccess()
}
} catch (error) {
if (error.name === "AuthenticationError") {
return { [FORM_ERROR]: "Sorry, those credentials are invalid" }
Expand All @@ -94,16 +73,20 @@ const HomePage: BlitzPage = () => {
<Head>
<title>Home</title>
</Head>
<Suspense fallback="Loading...">
<CurrentUser />
</Suspense>
<Suspense fallback="Loading...">
<UserInfo />
</Suspense>
<main>
<div>
<p>Hoi, welkom op deze pagina!</p>
<Link href="/Fietsen">
<a>Klik Deze Link!</a>
</Link>
</div>
</main>
</div>
)
}

HomePage.getLayout = (page) => <Layout title={"Home"}>{page}</Layout>
export default HomePage

0 comments on commit d7268e2

Please sign in to comment.