-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
27b1b9c
commit 7a0d75b
Showing
6 changed files
with
151 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Link, Form, useOutletContext } from "@remix-run/react"; | ||
import { Link, Form, useOutletContext, useActionData, redirect } from "@remix-run/react"; | ||
import { json } from "@remix-run/node"; | ||
import { useState } from "react"; | ||
import { createServerClient, parse, serialize } from '@supabase/ssr'; | ||
|
||
|
||
import googleIcon from "~/assets/oauth_providers/google-icon.png"; | ||
|
@@ -9,13 +11,67 @@ import githubIcon from "~/assets/oauth_providers/github-icon.png"; | |
|
||
import type { Database } from '../../database.types' | ||
import type { SupabaseClient, Provider } from '@supabase/supabase-js' | ||
import type { ActionFunctionArgs } from "@remix-run/node" | ||
|
||
|
||
export async function action({request}: ActionFunctionArgs) { | ||
const formData = await request.formData(); | ||
////return json({ xxx: formData.get("form_type") }); | ||
|
||
const cookies = parse(request.headers.get('Cookie') ?? '') | ||
const headers = new Headers() | ||
const supabase = createServerClient(process.env.SUPABASE_URL!, process.env.SUPABASE_ANON_KEY!, { | ||
cookies: { | ||
get(key) { | ||
return cookies[key] | ||
}, | ||
set(key, value, options) { | ||
headers.append('Set-Cookie', serialize(key, value, options)) | ||
}, | ||
remove(key, options) { | ||
headers.append('Set-Cookie', serialize(key, '', options)) | ||
}, | ||
}, | ||
}) | ||
|
||
// LOGIN | ||
if (formData.get("form_type") === "LOGIN"){ | ||
const {error} = await supabase.auth.signInWithPassword({ | ||
email: String(formData.get("email_input")), | ||
password: String(formData.get("password_input")) | ||
}) | ||
if (error) return json({ errorMessage: error.message }); | ||
return redirect("/"); | ||
} | ||
|
||
// REGISTER | ||
else if (formData.get("form_type") === "REGISTER") { | ||
const email = String(formData.get("reg_email_input")); | ||
const password = String(formData.get("reg_password_input")); | ||
|
||
let errorMessage: string | null = null; | ||
// validate info again on server side | ||
if (!email.includes("@") || !email.includes(".")) { | ||
errorMessage = "Invalid email address."; | ||
} | ||
else if (password.length < 6) { | ||
errorMessage = "Password should be at least 6 characters."; | ||
} | ||
////// more checks | ||
|
||
if (errorMessage) return json({ errorMessage }); | ||
return redirect("/"); | ||
} | ||
|
||
return json({ errorMessage: "Unknown form submitted." }); | ||
} | ||
|
||
export const loader = () => { | ||
|
||
return {}; | ||
}; | ||
|
||
export default function Login(){ | ||
const actionData = useActionData<typeof action>(); | ||
console.log(actionData?.errorMessage); ///////// keeps coming back to undefined | ||
|
||
|
||
const [isAtLogin, setIsAtLogin] = useState<boolean>(true); | ||
const { supabase } = useOutletContext<{ supabase: SupabaseClient<Database> }>() | ||
|
||
|
@@ -25,6 +81,18 @@ export default function Login(){ | |
}); | ||
}; | ||
|
||
|
||
const [email, setEmail] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
const [regPassword, setRegPassword] = useState(""); | ||
const [regConfirmPassword, setRegConfirmPassword] = useState(""); | ||
|
||
const fillDemoAcc = ()=>{ | ||
setEmail("[email protected]"); | ||
setPassword("sup3rs3cur3"); | ||
}; | ||
|
||
|
||
return <div className="flex flex-col md:flex-row justify-center items-center"> | ||
<div className="flex flex-col items-center"> | ||
<h1 className="text-xl mb-4">Continue with</h1> | ||
|
@@ -34,47 +102,64 @@ export default function Login(){ | |
<img src={googleIcon} alt="google"/> | ||
</button> | ||
<button className="oauth-provider-btn" | ||
onClick={()=>providerClicked("facebook")}> | ||
onClick={()=>providerClicked("facebook")}> | ||
<img src={facebookIcon} alt="facebook" /> | ||
</button> | ||
<button className="oauth-provider-btn" | ||
onClick={()=>providerClicked("github")}> | ||
onClick={()=>providerClicked("github")}> | ||
<img src={githubIcon} alt="github" /> | ||
</button> | ||
</div> | ||
</div> | ||
<div className="m-8 text-4xl mx-32 my-12">OR</div> | ||
|
||
<div className="relative text-center overflow-hidden border-2 border-color-2 rounded-lg w-80 h-96"> | ||
<button className="btn" onClick={()=>setIsAtLogin(!isAtLogin)} | ||
>{isAtLogin? "[Login]/Signup" : "Login/[Signup]"}</button> | ||
<div className="relative text-center overflow-hidden border-2 border-color-2 w-72 h-[480px]"> | ||
<button className="btn text-xs min-w-32 absolute top-2 right-2 z-50" onClick={()=>setIsAtLogin(!isAtLogin)} | ||
>{isAtLogin? "New user?" : "Login?"}</button> | ||
|
||
<div className={"auth-form " + (isAtLogin? "-translate-x-1/2" : "-translate-x-[200%]")}> | ||
<Form action="/events" method="post"> | ||
<div className={"auth-form right-1/2 " + (isAtLogin? "translate-x-1/2" : "-translate-x-full")}> | ||
<Form method="post"> | ||
<div className="flex flex-col"> | ||
<h1 className="text-center text-2xl pb-4">Login</h1> | ||
|
||
<input name="form_type" type="hidden" value="LOGIN"/> | ||
<label htmlFor="email_input">Email</label> | ||
<input id="email_input" type="email" autoComplete="email"/> | ||
<input name="email_input" id="email_input" | ||
type="email" autoComplete="email" required | ||
className="text-input" value={email} onChange={(e) => setEmail(e.target.value)}/> | ||
<label htmlFor="password_input">Password</label> | ||
<input id="password_input" type="password" autoComplete="current-password" /> | ||
<input name="password_input" id="password_input" | ||
type="password" autoComplete="current-password" | ||
className="text-input" value={password} onChange={(e) => setPassword(e.target.value)}/> | ||
|
||
<button type="submit" className='btn'>Log In</button> | ||
{actionData?.errorMessage ? ( | ||
<em>{actionData?.errorMessage}</em> | ||
) : null/* ///////// */} | ||
</div> | ||
</Form> | ||
<button className="underline">Fill demo account</button> | ||
<button className="btn text-xs mt-4" onClick={fillDemoAcc}>Fill demo account</button> | ||
</div> | ||
|
||
|
||
<div className={"auth-form " + (isAtLogin? "translate-x-full" : "-translate-x-1/2")}> | ||
<Form action="/events" method="post"> | ||
<div className={"auth-form left-1/2 " + (isAtLogin? "translate-x-full" : "-translate-x-1/2")}> | ||
<Form method="post"> | ||
<div className="flex flex-col"> | ||
|
||
<label htmlFor="email_input">Email</label> | ||
<input id="email_input" type="email" autoComplete="email"/> | ||
<label htmlFor="password_input">Password</label> | ||
<input id="password_input" type="password" autoComplete="new-password" /> | ||
<label htmlFor="confirm_password_input">Confirm password</label> | ||
<input id="confirm_password_input" type="password" autoComplete="new-password"/> | ||
<h1 className="text-center text-2xl pb-4">Register</h1> | ||
|
||
<input name="form_type" type="hidden" value="REGISTER"/> | ||
<label htmlFor="reg_email_input">Email</label> | ||
<input name="reg_email_input" id="reg_email_input" | ||
type="email" autoComplete="email" | ||
className="text-input" /> | ||
<label htmlFor="reg_password_input">Password</label> | ||
<input name="reg_password_input" id="reg_password_input" | ||
type="password" autoComplete="new-password" | ||
className="text-input" /> | ||
<label htmlFor="reg_confirm_password_input">Confirm password</label> | ||
<input name="reg_confirm_password_input" id="reg_confirm_password_input" | ||
type="password" autoComplete="new-password" | ||
className="text-input" /> | ||
|
||
<button type="submit" className='btn'>Sign up</button> | ||
</div> | ||
|
This file was deleted.
Oops, something went wrong.
This file contains 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 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,21 @@ | ||
import type { ActionFunctionArgs } from "@remix-run/node"; // or cloudflare/deno | ||
import { json } from "@remix-run/node"; // or cloudflare/deno | ||
import { Form, useActionData } from "@remix-run/react"; | ||
|
||
export async function action({ | ||
request, | ||
}: ActionFunctionArgs) { | ||
const body = await request.formData(); | ||
const name = body.get("visitorsName"); | ||
return json({ message: `Hello, ${name}` }); | ||
} | ||
|
||
export default function Invoices() { | ||
const data = useActionData<typeof action>(); | ||
return ( | ||
<Form method="post"> | ||
<input type="text" name="visitorsName" /> | ||
{data ? data.message : "Waiting..."} | ||
</Form> | ||
); | ||
} |
This file contains 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