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

feat: add login on game controller #58

Merged
merged 1 commit into from
Apr 17, 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
105 changes: 42 additions & 63 deletions src/pages/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { useState } from "react"
import { logout, signInWithEmail, signInWithGoogle } from "../services/authentication"
import { logout, signIn, signInWithGoogle } from "../services/authentication"
import { Toast } from "../components/Toast"
import { toast } from "react-toastify"

export const Auth = () => {
const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
type AuthProps = {
onSignIn: () => void
}

export const Auth = (props: AuthProps) => {
const [name, setName] = useState("")

const handleSignInWithEmail = (email: string, name: string, password: string) => {
signInWithEmail(email, name, password).then((response) => {
const handleSignIn = (name: string) => {
signIn(name).then((response) => {
if (response.success) {
toast.success("Connexion reussie")
} else {
toast.error("Erreur lors de la connexion")
}

props.onSignIn()
})
}

Expand All @@ -25,6 +29,8 @@ export const Auth = () => {
} else {
toast.error("Erreur lors de la connexion")
}

props.onSignIn()
})
}

Expand All @@ -33,66 +39,39 @@ export const Auth = () => {
}

return (
<div className="hero min-h-screen bg-base-200">
<div className="hero-content w-2/3 max-w-md flex-col lg:flex-row-reverse">
<div className="card shrink-0 w-full shadow-2xl bg-base-100">
<div className="card-body">
<div className="form-control">
<label className="label">
<span className="label-text">Nom</span>
</label>
<input
type="email"
className="input input-bordered rounded-full"
placeholder="nom"
onChange={(e) => setName(e.target.value)}
/>
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Email</span>
</label>
<input
type="email"
className="input input-bordered rounded-full"
placeholder="email"
onChange={(e) => setEmail(e.target.value)}
/>
</div>
<div className="form-control">
<label className="label">
<span className="label-text">Password</span>
</label>
<input
className="input input-bordered rounded-full"
placeholder="password"
type="password"
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div className="form-control mt-6">
<button
className="btn btn-neutral rounded-full"
onClick={() => handleSignInWithEmail(email, name, password)}
>
Sign In
</button>
</div>
<div className="min-h-screen flex flex-col justify-center items-center pt-16">
<div className="w-full lg:w-3/4 max-w-xl px-4 py-8 lg:px-8 space-y-4 rounded-box ">
<div className="card-body">
<div className="form-control">
<label className="label">
<span className="label-text">Nom</span>
</label>
<input
type="email"
className="input input-bordered rounded-full"
placeholder="nom"
onChange={(e) => setName(e.target.value)}
/>
</div>
<div className="form-control mt-6">
<button className="btn btn-neutral rounded-full" onClick={() => handleSignIn(name)}>
Sign In
</button>
</div>

<div className="divider">ou</div>
<div className="divider">ou</div>

<button className="btn btn-primary rounded-full" onClick={handleSignInWithGoogle}>
Sign in with Google
</button>
<button className="btn btn-primary rounded-full" onClick={handleSignInWithGoogle}>
Sign in with Google
</button>

<div className="mt-6">
Assez pour aujourd'hui...&nbsp;
<span>
<a className="link" onClick={handleLogout}>
logout
</a>
</span>
</div>
<div className="mt-6">
Assez pour aujourd'hui...&nbsp;
<span>
<a className="link" onClick={handleLogout}>
logout
</a>
</span>
</div>
</div>
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/pages/game/GameController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { LobbyRoom } from "./LobbyRoom"
import { toast } from "react-toastify"
import { Toast } from "../../components/Toast"
import { LoadingPage } from "../../components/LoadingPage"
import { Auth } from "../Auth"

export const GameController = () => {
const { gameId } = useParams()
const [user, setUser] = useState(getUserInfo())
const [gameState, setGameState] = useState(GameState.WAITING)
const [game, setGame] = useState({} as Game)
const [isLoading, setIsLoading] = useState(false)
Expand Down Expand Up @@ -98,6 +100,16 @@ export const GameController = () => {
})
}

if (!user.isAuth) {
return (
<Auth
onSignIn={() => {
setUser(getUserInfo())
}}
/>
)
}

return (
<div className="min-h-screen flex flex-col justify-center items-center pt-16">
{gameState === GameState.WAITING ? (
Expand Down
Loading