-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented complete auth system using Supabase #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
YousraElmag
wants to merge
7
commits into
main
Choose a base branch
from
auth-branch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68d6ad6
Implemented complete auth system using Supabase
144bb96
Add RoleSelection component
acce166
Refactored the auth system
20a2014
Style Register and Login forms using Tailwind CSS
325c256
add ci
a38a277
Merge branch 'main' into auth-branch
YousraElmag b18f009
fix ci.yml formatting
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| "use client"; | ||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| import { useEffect, useState } from "react"; | ||
| import { useRouter } from "next/navigation"; | ||
| import AuthRedirect from "@/components/AuthRedirect"; | ||
| import * as authServices from "@/lib/supabase/authServices"; | ||
|
|
||
| export default function OAuthCallback() { | ||
| const router = useRouter(); | ||
| const [readyToRedirect, setReadyToRedirect] = useState(false); | ||
| const [hasRole, setHasRole] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| async function handleCallback() { | ||
| try { | ||
| const user = await authServices.getUser(); | ||
|
|
||
| if (!user) { | ||
| router.replace("/auth/login"); | ||
| return; | ||
| } | ||
|
|
||
| const userTypeFromLocal = localStorage.getItem("UserRole"); | ||
|
|
||
| if (userTypeFromLocal) { | ||
| try { | ||
| await authServices.updateUserRole(userTypeFromLocal); | ||
| localStorage.removeItem("UserRole"); | ||
| setHasRole(true); | ||
| } catch { | ||
| alert("Failed to update user role"); | ||
| router.replace("/auth/login"); | ||
| return; | ||
| } | ||
| } else { | ||
| const role = user.user_metadata?.role; | ||
| if (role) { | ||
| setHasRole(true); | ||
| } else { | ||
| router.replace("/auth/register"); | ||
| } | ||
| } | ||
|
|
||
| setReadyToRedirect(true); | ||
| } catch { | ||
| router.replace("/auth/login"); | ||
| } | ||
| } | ||
|
|
||
| handleCallback(); | ||
| }, [router]); | ||
|
|
||
| if (!readyToRedirect) return <p>Loading...</p>; | ||
| if (hasRole) return <AuthRedirect />; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { useForgotPassword } from "./useForgotPassword"; | ||
|
|
||
| export default function ForgotPasswordPage() { | ||
| const [email, setEmail] = useState(""); | ||
| const { sendResetLink, loading, error, success } = useForgotPassword(); | ||
|
|
||
| const handleSubmit = async (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| await sendResetLink(email); | ||
| }; | ||
|
|
||
| return ( | ||
| <main className="min-h-screen flex items-center justify-center bg-gray-100 p-4"> | ||
| <div className="bg-white p-6 rounded shadow-md max-w-md w-full"> | ||
| <h1 className="text-xl font-semibold mb-4">Forgot your password?</h1> | ||
| <form onSubmit={handleSubmit}> | ||
| <input | ||
| type="email" | ||
| placeholder="Enter your email" | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| required | ||
| className="w-full p-2 border rounded mb-4" | ||
| /> | ||
| <button | ||
| type="submit" | ||
| disabled={loading} | ||
| className="w-full bg-blue-600 text-white py-2 rounded hover:bg-blue-700" | ||
| > | ||
| {loading ? "Sending..." : "Send Reset Link"} | ||
| </button> | ||
| </form> | ||
| {error && <p className="text-red-500 mt-2">{error}</p>} | ||
| {success && ( | ||
| <p className="text-green-600 mt-2"> | ||
| A password reset link has been sent to your email. | ||
| </p> | ||
| )} | ||
| </div> | ||
| </main> | ||
| ); | ||
| } |
31 changes: 31 additions & 0 deletions
31
apps/web/src/app/auth/forgot-password/useForgotPassword.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { sendPasswordResetLink } from "@/lib/supabase/authServices"; | ||
|
|
||
| export const useForgotPassword = () => { | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [success, setSuccess] = useState(false); | ||
|
|
||
| const sendResetLink = async (email: string) => { | ||
| setLoading(true); | ||
| setError(null); | ||
| setSuccess(false); | ||
|
|
||
| try { | ||
| await sendPasswordResetLink(email); | ||
| setSuccess(true); | ||
| } catch (err: unknown) { | ||
| if (err instanceof Error) { | ||
| setError(err.message); | ||
| } else { | ||
| setError("Something went wrong"); | ||
| } | ||
| return null; | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return { sendResetLink, loading, error, success }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| "use client"; | ||
| import { signInWithGoogle } from "@/lib/supabase/authServices"; | ||
| import Image from "next/image"; | ||
| interface GoogleLoginButtonProps { | ||
| role?: "client" | "provider"; | ||
| } | ||
|
|
||
| export default function GoogleLoginButton({ role }: GoogleLoginButtonProps) { | ||
| const handleGoogleSignIn = async () => { | ||
| try { | ||
| await signInWithGoogle(role); | ||
| } catch (err: unknown) { | ||
| if (err instanceof Error) { | ||
| alert(err.message); | ||
| } else { | ||
| alert("Failed to initiate Google sign-in"); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <button | ||
| onClick={handleGoogleSignIn} | ||
| className="flex items-center justify-center gap-2 w-full py-3 bg-white text-gray-800 border border-gray-300 rounded-md shadow-md hover:bg-gray-100 transition text-sm font-semibold" | ||
| > | ||
| <Image | ||
| src="/Googleicon.png" | ||
| alt="Google icon" | ||
| width={16} | ||
| height={16} | ||
| className="mr-2" | ||
| /> | ||
| Continue with Google | ||
| </button> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { useLogin } from "./useLogin"; | ||
| import Link from "next/link"; | ||
| import GoogleLoginButton from "./GoogleLoginButton"; | ||
| import AuthRedirect from "@/components/AuthRedirect"; | ||
| import Logo from "../../../components/logo"; | ||
| import Back from "../../../components/Backbutton"; | ||
| export default function LoginForm() { | ||
| const [email, setEmail] = useState(""); | ||
| const [password, setPassword] = useState(""); | ||
| const [loginSuccess, setLoginSuccess] = useState(false); | ||
| const { login, loading, error } = useLogin(); | ||
|
|
||
| const handleSubmit = async (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| const result = await login(email, password); | ||
| if (result) { | ||
| setLoginSuccess(true); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Logo /> | ||
|
|
||
| <div className="bg-[#cce6ff] rounded-xl w-[429px] mx-auto my-10 p-8 shadow-md space-y-6"> | ||
| <form onSubmit={handleSubmit} className="space-y-6"> | ||
| {loginSuccess && <AuthRedirect />} | ||
|
|
||
| <h3 className="text-center text-2xl font-bold mb-5 text-black"> | ||
| Login to your account | ||
| </h3> | ||
|
|
||
| <input | ||
| type="email" | ||
| placeholder="Email" | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| required | ||
| className="w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-400 bg-white text-sm" | ||
| /> | ||
| <input | ||
| type="password" | ||
| placeholder="Password" | ||
| value={password} | ||
| onChange={(e) => setPassword(e.target.value)} | ||
| required | ||
| className="w-full px-4 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-400 bg-white text-sm" | ||
| /> | ||
|
|
||
| {error && ( | ||
| <p className="text-red-600 text-sm font-semibold">{error}</p> | ||
| )} | ||
|
|
||
| <button | ||
| type="submit" | ||
| disabled={loading} | ||
| className="w-full bg-green-600 hover:bg-green-700 text-white py-3 rounded-md text-base font-semibold shadow-md transition" | ||
| > | ||
| {loading ? "Logging in ..." : "Login"} | ||
| </button> | ||
|
|
||
| <div className="text-center mt-2 text-sm text-gray-700"> | ||
| <Link | ||
| href="/auth/forgot-password" | ||
| className="text-blue-600 font-medium hover:underline" | ||
| > | ||
| Forgot your password? | ||
| </Link> | ||
| </div> | ||
|
|
||
| <div className="flex items-center my-6 text-gray-600 text-sm"> | ||
| <div className="flex-grow h-[1px] bg-gray-300"></div> | ||
| <span className="mx-3">or</span> | ||
| <div className="flex-grow h-[1px] bg-gray-300"></div> | ||
| </div> | ||
|
|
||
| <GoogleLoginButton /> | ||
| </form> | ||
| <Back /> | ||
| </div> | ||
| </> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import LoginForm from "../login/LoginForm"; | ||
|
|
||
| export default function LoginPage() { | ||
| return ( | ||
| <div className="min-h-screen flex items-center justify-center bg-gray-50"> | ||
| <LoginForm /> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| "use client"; | ||
| import { useState } from "react"; | ||
| import { signInWithEmail } from "@/lib/supabase/authServices"; | ||
|
|
||
| export const useLogin = () => { | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| const login = async (email: string, password: string) => { | ||
| setLoading(true); | ||
| setError(null); | ||
| try { | ||
| const data = await signInWithEmail(email, password); | ||
| return data; | ||
| } catch (err: unknown) { | ||
| if (err instanceof Error) { | ||
| setError(err.message); | ||
| } else { | ||
| setError("Something went wrong"); | ||
| } | ||
| return null; | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return { login, loading, error }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you explain to me what is happening in this page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This page checks if the user is signed in after Google login.
If the user has no role, it sends them to choose one.
If signed in with a role, it sends them to the home page (dashboard).
If not signed in, it sends them back to login.
After we finish building the dashboard pages, we will redirect the user to their dashboard instead of the login page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
You can make a placeholder pages with only one text e.g. "Dashboard for Client" , "Dashboard for Provider"
also if the user is new instead of going to the dashboard we can send them to the profile page to complete the information. and maybe we can have in supabase a field (isOnboarded?) to decide this