diff --git a/leaky-ships/pages/api/auth/[...nextauth].ts b/leaky-ships/pages/api/auth/[...nextauth].ts index 10ff3dc..c5872ec 100644 --- a/leaky-ships/pages/api/auth/[...nextauth].ts +++ b/leaky-ships/pages/api/auth/[...nextauth].ts @@ -48,6 +48,13 @@ const options: NextAuthOptions = { return session }, }, + pages: { + signIn: "/login", + signOut: "/logout", + // error: '/auth/error', // Error code passed in query string as ?error= + // verifyRequest: '/auth/verify-request', // (used for check email message) + // newUser: '/auth/new-user' // New users will be directed here on first sign in (leave the property out if not of interest) + }, } export { options as authOptions } diff --git a/leaky-ships/pages/login.tsx b/leaky-ships/pages/login.tsx index a2305b8..4d8486d 100644 --- a/leaky-ships/pages/login.tsx +++ b/leaky-ships/pages/login.tsx @@ -1,144 +1,142 @@ -import { faWifiExclamation } from "@fortawesome/pro-duotone-svg-icons" -import { - faArrowLeft, - faCheck, - faSpinnerThird, - faXmark, -} from "@fortawesome/pro-solid-svg-icons" +import { faLeftLong } from "@fortawesome/pro-solid-svg-icons" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" -import classNames from "classnames" -import { FormEvent, useState } from "react" +import { signIn, useSession } from "next-auth/react" +import { useRouter } from "next/router" +import { useEffect, useState } from "react" +import { toast } from "react-toastify" -enum ProcessStates { - "waiting", - "loading", - "success", - "wrong", - "error", -} -const messages: { [key in ProcessStates]: string } = { - [ProcessStates.waiting]: "Enter Login Details", - [ProcessStates.loading]: "Logging in...", - [ProcessStates.success]: "Done!", - [ProcessStates.wrong]: "Wrong username or password", - [ProcessStates.error]: "An error occurred!", -} -const icons = { - [ProcessStates.loading]: faSpinnerThird, - [ProcessStates.success]: faCheck, - [ProcessStates.wrong]: faXmark, - [ProcessStates.error]: faWifiExclamation, -} -const iconClasses = { - [ProcessStates.loading]: "animate-spin", - [ProcessStates.success]: "text-green-500", - [ProcessStates.wrong]: "text-red-500", - [ProcessStates.error]: "animate-pulse text-amber-500 !text-8xl", +type SignInErrorTypes = + | "Signin" + | "OAuthSignin" + | "OAuthCallback" + | "OAuthCreateAccount" + | "EmailCreateAccount" + | "Callback" + | "OAuthAccountNotLinked" + | "EmailSignin" + | "CredentialsSignin" + | "SessionRequired" + | "default" + +const errors: Record = { + Signin: "Try signing in with a different account.", + OAuthSignin: "Try signing in with a different account.", + OAuthCallback: "Try signing in with a different account.", + OAuthCreateAccount: "Try signing in with a different account.", + EmailCreateAccount: "Try signing in with a different account.", + Callback: "Try signing in with a different account.", + OAuthAccountNotLinked: + "To confirm your identity, sign in with the same account you used originally.", + EmailSignin: "The e-mail could not be sent.", + CredentialsSignin: + "Sign in failed. Check the details you provided are correct.", + SessionRequired: "Please sign in to access this page.", + default: "Unable to sign in.", } function Login() { - const [username, setUsername] = useState("") - const [password, setPassword] = useState("") + const [email, setEmail] = useState("") + const { status } = useSession() + const router = useRouter() - const [state, setState] = useState(ProcessStates.waiting) - const [error, setError] = useState("") + const errorType = router.query.error as SignInErrorTypes - const elem = () => { - if (state === ProcessStates.waiting) - return ( -
-
- setUsername(e.target.value)} - /> -
+ useEffect(() => { + if (!errorType) return + toast.error(errors[errorType] ?? errors.default, { theme: "colored" }) + }, [errorType]) -
- setPassword(e.target.value)} - /> -
-
- -
-
- ) + useEffect(() => { + if (status === "authenticated") router.push("/") + }, [router, status]) - return ( - <> -
- -
-
- -
- - ) - } - - async function login(e: FormEvent) { - e.preventDefault() - setState(ProcessStates.loading) - await fetch("/api/login", { - method: "POST", - body: JSON.stringify({ username, password }), - }) - .then((res) => { - if (res.status === 200) setState(ProcessStates.success) - if (res.status === 401) setState(ProcessStates.wrong) - }) - .catch((err: Error) => { - setState(ProcessStates.error) - setError(err.message) - }) + function login(provider: "email" | "azure-ad") { + return () => { + signIn(provider, { email, callbackUrl: "/" }) + } } return (
-
-
-
- Avatar -

Leaky Ships

- - {error ? error : messages[state]} - -
- {elem()} +
+
+ Avatar +

Leaky Ships

+ Choose Login Method
+ {errorType &&
} +
+
+ + setEmail(e.target.value)} + /> + +
+ +
+
+ or +
+
+ +
+ + Gewerbliche Berufsbildende Schulen + + +
+
+ {errorType &&
} + {errorType && ( +
+ +
+ )}
) diff --git a/leaky-ships/pages/logout.tsx b/leaky-ships/pages/logout.tsx index 5269a2f..aeb161b 100644 --- a/leaky-ships/pages/logout.tsx +++ b/leaky-ships/pages/logout.tsx @@ -1,15 +1,14 @@ -import { faSpinnerThird } from "@fortawesome/pro-solid-svg-icons" -import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" +import { signOut, useSession } from "next-auth/react" import { useRouter } from "next/router" import React, { useEffect } from "react" function Logout() { + const { status } = useSession() const router = useRouter() useEffect(() => { - const timeout = setTimeout(() => router.push("/dev"), 2000) - return () => clearTimeout(timeout) - }, [router]) + if (status === "unauthenticated") router.push("/signin") + }, [router, status]) return (
@@ -23,12 +22,17 @@ function Logout() { alt="Avatar" />

Leaky Ships

- Logging out... + Signout +
+
+ Are you sure you want to sign out? +
-
diff --git a/leaky-ships/pages/start.tsx b/leaky-ships/pages/start.tsx index 277aa47..cb4153f 100644 --- a/leaky-ships/pages/start.tsx +++ b/leaky-ships/pages/start.tsx @@ -138,16 +138,30 @@ export default function Start() {
- +
+ + {!session?.user.id && ( + + )} +
gameFetch()} diff --git a/leaky-ships/public/images/Microsoft_icon.svg b/leaky-ships/public/images/Microsoft_icon.svg new file mode 100644 index 0000000..13307b7 --- /dev/null +++ b/leaky-ships/public/images/Microsoft_icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/leaky-ships/public/images/logo-gbs.png b/leaky-ships/public/images/logo-gbs.png new file mode 100644 index 0000000..a1f3090 Binary files /dev/null and b/leaky-ships/public/images/logo-gbs.png differ