Skip to content

Commit

Permalink
update login messages
Browse files Browse the repository at this point in the history
  • Loading branch information
chaseWillden committed Oct 18, 2024
1 parent dc8675a commit 73f8dc2
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 33 deletions.
157 changes: 157 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"mongodb": "^6.8.0",
"nanoid": "^5.0.7",
"next": "14.2.15",
"next-auth": "^4.24.8",
"openai": "^4.67.3",
"prismjs": "^1.29.0",
"react": "^18",
Expand Down
32 changes: 32 additions & 0 deletions src/app/api/auth/assume/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { PasswordAuth } from "@/models/auth.model"
import {
createToken,
loginPasswordAuth,
} from "@/server/services/auth/password.auth.service"
import { failure, success, toJson } from "@/server/services/request.service"
import { getUserByEmail } from "@/server/services/user.service"
import { NextRequest } from "next/server"

export const POST = async (req: NextRequest) => {
const body = await toJson<Omit<PasswordAuth, "id">>(req)
const isSuccessful = await loginPasswordAuth(body)
if (isSuccessful) {
const user = await getUserByEmail(body.email)

if (!user) return failure("User not found")

const token = await createToken(user!)
const res = success({ user, token })

res.cookies.set("auth", token, {
path: "/",
expires: new Date(Date.now() + 1000 * 60 * 60 * 12),
secure: true,
httpOnly: true,
})

return res
} else {
return failure(isSuccessful)
}
}
2 changes: 1 addition & 1 deletion src/app/api/auth/login/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { NextRequest } from "next/server"
export const POST = async (req: NextRequest) => {
const body = await toJson<Omit<PasswordAuth, "id">>(req)
const isSuccessful = await loginPasswordAuth(body)
if (!isSuccessful) {
if (isSuccessful) {
const user = await getUserByEmail(body.email)

if (!user) return failure("User not found")
Expand Down
10 changes: 0 additions & 10 deletions src/app/assignments/page.tsx

This file was deleted.

27 changes: 24 additions & 3 deletions src/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@ import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { useInstallHook } from "@/hooks/use-install.hook"
import { useLoginMutation } from "@/redux/services/auth.api"
import { Spinner } from "@material-tailwind/react"
import { Spinner, Typography } from "@material-tailwind/react"
import React, { useState } from "react"

export default function Page() {
const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
const [login, { isLoading }] = useLoginMutation()
const [hasError, setHasError] = useState(false)

useInstallHook()

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()

await login({ email, password })
try {
const results = await login({ email, password })

if (results.error) {
setHasError(true)
console.log(results.error)
return
}
} catch (error) {
setHasError(true)
return
}

setEmail("")
setPassword("")
Expand Down Expand Up @@ -71,7 +83,16 @@ export default function Page() {
value={password}
/>
</div>
<Button type="submit" className="w-full" disabled={isLoading}>
{hasError && (
<Typography variant="small" className="text-red-500">
Invalid email or password
</Typography>
)}
<Button
type="submit"
className="w-full"
disabled={isLoading && !hasError}
>
{isLoading && <Spinner className="w-4 h-4 mr-2" />} Login
</Button>
</div>
Expand Down
2 changes: 2 additions & 0 deletions src/components/icons/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BoltIcon,
ChatBubbleLeftIcon,
CogIcon,
DocumentIcon,
Expand Down Expand Up @@ -38,6 +39,7 @@ export const AvailableIcons = {
CogIcon,
UsersIcon,
HeadingIcon,
BoltIcon
}

export const getIcon = (name?: string) => {
Expand Down
9 changes: 1 addition & 8 deletions src/components/sidenav/apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
Calendar,
ChevronDownIcon,
LayoutDashboardIcon,
ListCheckIcon,
School2Icon,
School2Icon
} from "lucide-react"
import Link from "next/link"
import { useState } from "react"
Expand Down Expand Up @@ -48,12 +47,6 @@ export const SideNavApps = ({ isOpen, onToggle, listItemClassName }: Props) => {
href: "/calendar",
isVisible: true,
},
{
name: "Assignments",
icon: <ListCheckIcon className="h-5 w-5" />,
href: "/assignments",
isVisible: true,
},
{
name: "System Settings",
icon: <GearIcon className="h-5 w-5" />,
Expand Down
1 change: 1 addition & 0 deletions src/redux/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const api = createApi({
* A bare bones base query would just be `baseQuery: fetchBaseQuery({ baseUrl: '/' })`
*/
baseQuery: baseQueryWithRetry,

/**
* Tag types must be defined in the original API definition
* for any tags that would be provided by injected endpoints
Expand Down
3 changes: 3 additions & 0 deletions src/redux/services/auth.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export const authApi = api.injectEndpoints({
method: "POST",
body,
}),
extraOptions: {
maxRetries: 1,
},
}),
signup: build.mutation<any, any>({
query: (body) => ({
Expand Down
3 changes: 3 additions & 0 deletions src/redux/services/user-settings.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export const userSettingsApi = api.injectEndpoints({
method: "GET",
}),
providesTags: ["UserSettings"],
extraOptions: {
maxRetries: 1,
},
}),

updateUserSettings: build.mutation<UserSettings, UpdateUserSettingsPayload>(
Expand Down
Loading

0 comments on commit 73f8dc2

Please sign in to comment.