Fix login with redirect
This commit is contained in:
parent
aafec28885
commit
3a73a327c3
7 changed files with 41 additions and 42 deletions
|
|
@ -14,7 +14,7 @@ describe("LoginWithGoogle", () => {
|
|||
loginWithGoogle: mockLoginWithGoogle,
|
||||
});
|
||||
|
||||
render(<LoginWithGoogle redirectUrl={null} />);
|
||||
render(<LoginWithGoogle />);
|
||||
|
||||
const button = screen.getByRole("button", {
|
||||
name: /Continuer avec Google/i,
|
||||
|
|
@ -28,7 +28,7 @@ describe("LoginWithGoogle", () => {
|
|||
loginWithGoogle: mockLoginWithGoogle,
|
||||
});
|
||||
|
||||
render(<LoginWithGoogle redirectUrl={null} />);
|
||||
render(<LoginWithGoogle />);
|
||||
|
||||
const button = screen.getByRole("button", {
|
||||
name: /Continuer avec Google/i,
|
||||
|
|
|
|||
|
|
@ -1,12 +1,8 @@
|
|||
import "./login-with-google.css";
|
||||
import { useLoginGoogle } from "../../hooks/auth";
|
||||
|
||||
export function LoginWithGoogle({
|
||||
redirectUrl,
|
||||
}: {
|
||||
redirectUrl: string | null;
|
||||
}) {
|
||||
const { loginWithGoogle } = useLoginGoogle({ redirectUrl });
|
||||
export function LoginWithGoogle() {
|
||||
const { loginWithGoogle } = useLoginGoogle();
|
||||
|
||||
return (
|
||||
<button className="gsi-material-button" onClick={() => loginWithGoogle()}>
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { useSession } from "../contexts/SessionContext";
|
||||
import { Navigate, Outlet } from "react-router-dom";
|
||||
import { Navigate, Outlet, useSearchParams } from "react-router-dom";
|
||||
import { match } from "ts-pattern";
|
||||
|
||||
export const PublicRoute = () => {
|
||||
const { session } = useSession();
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
const [searchParams] = useSearchParams();
|
||||
const redirectUrl = searchParams.get("redirect");
|
||||
|
||||
if (redirectUrl) {
|
||||
localStorage.setItem("redirectUrl", redirectUrl);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
setIsLoading(false);
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ interface AuthResponse {
|
|||
session: Session | null;
|
||||
}
|
||||
|
||||
export function useSignUp() {
|
||||
export function useSignUp({ redirectUrl }: { redirectUrl: string | null }) {
|
||||
const navigate = useNavigate();
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
const { signUpToStream } = useSignUpToStream();
|
||||
|
|
@ -76,7 +76,12 @@ export function useSignUp() {
|
|||
return response;
|
||||
},
|
||||
onSuccess: () => {
|
||||
navigate("/");
|
||||
if (redirectUrl) {
|
||||
localStorage.removeItem("redirectUrl");
|
||||
navigate(decodeURIComponent(redirectUrl));
|
||||
} else {
|
||||
navigate("/");
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
const errMap: Record<string, string> = {};
|
||||
|
|
@ -144,6 +149,7 @@ export function useLoginEmail({ redirectUrl }: { redirectUrl: string | null }) {
|
|||
},
|
||||
onSuccess: () => {
|
||||
if (redirectUrl) {
|
||||
localStorage.removeItem("redirectUrl");
|
||||
navigate(decodeURIComponent(redirectUrl));
|
||||
} else {
|
||||
navigate("/");
|
||||
|
|
@ -172,28 +178,13 @@ export function useLoginEmail({ redirectUrl }: { redirectUrl: string | null }) {
|
|||
return { mutate, isPending, errors };
|
||||
}
|
||||
|
||||
export function useLoginGoogle({
|
||||
redirectUrl,
|
||||
}: {
|
||||
redirectUrl: string | null;
|
||||
}) {
|
||||
console.log({
|
||||
redirectTo: redirectUrl
|
||||
? `${
|
||||
window.location.origin
|
||||
}/login-with-oauth?redirect=${encodeURIComponent(redirectUrl)}`
|
||||
: `${window.location.origin}/login-with-oauth`,
|
||||
});
|
||||
export function useLoginGoogle() {
|
||||
const { mutate } = useMutation({
|
||||
mutationFn: async () => {
|
||||
const { data, error } = await supabase.auth.signInWithOAuth({
|
||||
provider: "google",
|
||||
options: {
|
||||
redirectTo: redirectUrl
|
||||
? `${
|
||||
window.location.origin
|
||||
}/login-with-oauth?redirect=${encodeURIComponent(redirectUrl)}`
|
||||
: `${window.location.origin}/login-with-oauth`,
|
||||
redirectTo: `${window.location.origin}/login-with-oauth`,
|
||||
},
|
||||
});
|
||||
if (error) throw error;
|
||||
|
|
|
|||
|
|
@ -5,13 +5,17 @@ import { Label, Input, TextField, FieldError } from "@ui/ui-library/field";
|
|||
import { useLoginEmail } from "@ui/hooks/auth";
|
||||
import { Form } from "@ui/ui-library/form";
|
||||
import { LoginWithGoogle } from "@ui/components/BrandButtons/LoginWithGoogle";
|
||||
import { Link, useSearchParams } from "react-router-dom";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
export function LoginPage() {
|
||||
const [searchParams] = useSearchParams();
|
||||
const redirectUrl = searchParams.get("redirect");
|
||||
|
||||
const { mutate: login, isPending, errors } = useLoginEmail({ redirectUrl });
|
||||
const redirectUrl = localStorage.getItem("redirectUrl");
|
||||
const {
|
||||
mutate: login,
|
||||
isPending,
|
||||
errors,
|
||||
} = useLoginEmail({
|
||||
redirectUrl: redirectUrl ?? null,
|
||||
});
|
||||
const [formData, setFormData] = useState({
|
||||
email: "",
|
||||
password: "",
|
||||
|
|
@ -116,7 +120,7 @@ export function LoginPage() {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<LoginWithGoogle redirectUrl={redirectUrl} />
|
||||
<LoginWithGoogle />
|
||||
|
||||
<p className="text-center text-sm text-slate-600 dark:text-slate-400">
|
||||
Pas encore de compte ?{" "}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { useEffect } from "react";
|
||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useSession } from "@ui/contexts/SessionContext";
|
||||
import { useSignUpToStream } from "@ui/hooks/auth";
|
||||
|
||||
|
|
@ -7,13 +7,13 @@ export const OAuthSigninPage = () => {
|
|||
const navigate = useNavigate();
|
||||
const { session } = useSession();
|
||||
const { signUpToStream } = useSignUpToStream();
|
||||
const [searchParams] = useSearchParams();
|
||||
const redirectUrl = searchParams.get("redirect");
|
||||
const redirectUrl = localStorage.getItem("redirectUrl");
|
||||
useEffect(() => {
|
||||
const interval = setInterval(() => {
|
||||
if (session) {
|
||||
signUpToStream(session.access_token);
|
||||
if (redirectUrl) {
|
||||
localStorage.removeItem("redirectUrl");
|
||||
navigate(decodeURIComponent(redirectUrl));
|
||||
} else {
|
||||
navigate("/");
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Button } from "@ui/ui-library/button";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
import { Link, useNavigate, useSearchParams } from "react-router-dom";
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
import { useState } from "react";
|
||||
import { Label, Input, TextField, FieldError } from "@ui/ui-library/field";
|
||||
import { useSignUp } from "@ui/hooks/auth";
|
||||
|
|
@ -10,9 +10,10 @@ import { LoginWithGoogle } from "@ui/components/BrandButtons/LoginWithGoogle";
|
|||
|
||||
export function SignUpPage() {
|
||||
const navigate = useNavigate();
|
||||
const [searchParams] = useSearchParams();
|
||||
const redirectUrl = searchParams.get("redirect");
|
||||
const { mutate: signUp, isPending } = useSignUp();
|
||||
const redirectUrl = localStorage.getItem("redirectUrl");
|
||||
const { mutate: signUp, isPending } = useSignUp({
|
||||
redirectUrl: redirectUrl ?? null,
|
||||
});
|
||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
|
|
@ -248,7 +249,7 @@ export function SignUpPage() {
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<LoginWithGoogle redirectUrl={redirectUrl} />
|
||||
<LoginWithGoogle />
|
||||
|
||||
<p className="text-center text-sm text-slate-600 dark:text-slate-400">
|
||||
Déjà un compte ?{" "}
|
||||
|
|
|
|||
Loading…
Reference in a new issue