Skip to content
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

Forgot pwd fix #208

Merged
merged 6 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions nepalingo-web/src/hooks/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from "react";
import React, { useContext, useState, useEffect, createContext } from "react";
import {
AuthResponse,
AuthTokenResponsePassword,
Session,
SignUpWithPasswordCredentials,
User,
} from "@supabase/supabase-js";
import { useContext, useState, useEffect, createContext } from "react";
import { supabaseClient } from "@/config/supabase-client";

type AuthContextProps = {
Expand All @@ -18,8 +17,8 @@ type AuthContextProps = {
data: SignUpWithPasswordCredentials,
) => Promise<AuthTokenResponsePassword>;
resetPasswordEmail: (email: string) => Promise<{ error: Error | null }>;
resetPassword: (password: string) => Promise<{ error: Error | null }>;
};

const AuthContext = createContext<AuthContextProps>({
session: null,
user: null,
Expand All @@ -30,12 +29,11 @@ const AuthContext = createContext<AuthContextProps>({
supabaseClient.auth.resetPasswordForEmail(email, {
redirectTo: "https://www.nepalingo.com/reset-password",
}),
resetPassword: (password) => supabaseClient.auth.updateUser({ password }),
});

export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
const [user, setUser] = useState<User>();
const [session, setSession] = useState<Session | null>();
const [user, setUser] = useState<User | null>(null);
const [session, setSession] = useState<Session | null>(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
Expand All @@ -46,14 +44,14 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
} = await supabaseClient.auth.getSession();
if (error) throw error;
setSession(session);
setUser(session?.user);
setUser(session?.user || null);
setLoading(false);
};

const { data: listener } = supabaseClient.auth.onAuthStateChange(
(_event, session) => {
setSession(session);
setUser(session?.user);
setUser(session?.user || null);
setLoading(false);
},
);
Expand All @@ -75,7 +73,6 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
supabaseClient.auth.resetPasswordForEmail(email, {
redirectTo: "https://www.nepalingo.com/reset-password",
}),
resetPassword: (password) => supabaseClient.auth.updateUser({ password }),
};

return (
Expand Down
62 changes: 29 additions & 33 deletions nepalingo-web/src/pages/ResetPassword.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import React, { useState } from "react";
import { useAuth } from "@/hooks/Auth";
import { useNavigate } from "react-router-dom";
import CustomTextInput from "@/components/CustomTextInput";
import Button from "@/components/Button";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faLock, faEye, faEyeSlash } from "@fortawesome/free-solid-svg-icons";
import { supabaseClient } from "@/config/supabase-client";

const ResetPassword: React.FC = () => {
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [message, setMessage] = useState("");
const [error, setError] = useState("");
const [showPassword, setShowPassword] = useState(false);
const { resetPassword } = useAuth();
const [showNewPassword, setShowNewPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const navigate = useNavigate();

const handlePasswordReset = (event: React.FormEvent) => {
const handlePasswordReset = async (event: React.FormEvent) => {
event.preventDefault();

if (password !== confirmPassword) {
setError("Passwords do not match");
return;
}

resetPassword(password).then(({ error }) => {
if (error) {
setError(error.message);
setMessage("");
} else {
setMessage("Password reset successful! Go to ");
setError("");
}
// Update the password using the Supabase client
const { error: updateError } = await supabaseClient.auth.updateUser({
password,
});

if (updateError) {
setError(updateError.message);
} else {
navigate("/login", {
state: {
message:
"Password reset successful! Please log in with your new password.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's neat

},
});
}
};

return (
<div className="flex items-center justify-center min-h-screen bg-black">
<div className="bg-black p-8 rounded shadow-md w-full max-w-md">
<h2 className="text-4xl text-primary mb-6">Reset Password</h2>
<form onSubmit={handlePasswordReset} className="space-y-4">
<div className="flex flex-col">
<div className="flex flex-col relative">
<CustomTextInput
label="New Password"
name="NewPassword"
placeholder="eg., @ReallySecure07"
type={showPassword ? "text" : "password"}
placeholder="e.g., @ReallySecure07"
type={showNewPassword ? "text" : "password"}
value={password}
onChange={(e) => setPassword(e.target.value)}
iconProps={{
Expand All @@ -55,16 +60,17 @@ const ResetPassword: React.FC = () => {
containerStyle="h-12"
/>
<FontAwesomeIcon
icon={showPassword ? faEyeSlash : faEye}
icon={showNewPassword ? faEyeSlash : faEye}
className="text-white absolute right-3 bottom-4 cursor-pointer"
onClick={() => setShowPassword(!showPassword)}
onClick={() => setShowNewPassword(!showNewPassword)}
/>
</div>
<div className="flex flex-col">

<div className="flex flex-col relative">
<CustomTextInput
label="Confirm New Password"
name="confirm password"
type={showPassword ? "text" : "password"}
type={showConfirmPassword ? "text" : "password"}
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
iconProps={{
Expand All @@ -75,29 +81,19 @@ const ResetPassword: React.FC = () => {
containerStyle="h-12"
/>
<FontAwesomeIcon
icon={showPassword ? faEyeSlash : faEye}
icon={showConfirmPassword ? faEyeSlash : faEye}
className="text-white absolute right-3 bottom-4 cursor-pointer"
onClick={() => setShowPassword(!showPassword)}
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
/>
</div>

<Button
type="submit"
className="w-full bg-blue-500 text-white py-2 rounded"
>
Reset Password
</Button>
</form>
{message && (
<div className="mt-4 text-green-500">
{message}{" "}
<button
onClick={() => navigate("/login")}
className="underline text-white"
>
Login
</button>
</div>
)}
{error && <p className="text-red-500 mt-4">{error}</p>}
</div>
</div>
Expand Down
Loading