-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add social OAuth (GitHub and Discord) #49
Open
martakam
wants to merge
14
commits into
main
Choose a base branch
from
feat/social-oauth
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 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae6e53b
build: add simple icons react package to frontend
martakam 1dc44e3
feat: add GitHub OAuth
martakam df9eb27
feat: give all buttons cursor-pointer
martakam 39ef62a
feat: add Collapsible component from shadcn
martakam 7fbd1d9
feat: add sign in with Discord
martakam e77d314
feat: move additional OAuths to own component
martakam aa0a6a6
docs: add instructions on social OAuth using GitHub and Discord
martakam 4f9c0bf
Merge branch 'main' into feat/social-oauth
martakam 5faea8f
docs: add info about app URL env variable
martakam 7bd11de
refactor: move Discord login out of Collapsible
martakam 7482d93
fix: redirect user if already logged in
martakam 1f5f82f
fix: build callback URL with client-side data as fallback
martakam e55fd2a
Merge branch 'main' into feat/social-oauth
martakam 63bbef4
Merge branch 'main' into feat/social-oauth
martakam 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
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| VITE_APP_URL=http://localhost:5173 | ||
|
|
||
| # Must be changed to http://it2810-37.idi.ntnu.no:3001/ for production | ||
| VITE_API_URL = http://localhost:3001 |
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,50 @@ | ||
| import { | ||
| Collapsible, | ||
| CollapsibleContent, | ||
| CollapsibleTrigger, | ||
| } from "~/components/ui/Collapsible"; | ||
| import { ChevronDown, ChevronUp } from "lucide-react"; | ||
| import { SiDiscord } from "@icons-pack/react-simple-icons"; | ||
| import { Button } from "~/components/ui/Button"; | ||
| import { useState } from "react"; | ||
| import { authClient } from "~/api/auth"; | ||
|
|
||
| function SocialLogIns({ logInType }: { logInType: "login" | "signup" }) { | ||
| const [collapsibleOpen, setCollapsibleOpen] = useState(false); | ||
|
|
||
| return ( | ||
| <Collapsible | ||
| className="space-y-4" | ||
| onOpenChange={(value) => setCollapsibleOpen(value)} | ||
| > | ||
| <div className="flex justify-center items-center gap-4 font-semibold"> | ||
| More options | ||
| <CollapsibleTrigger> | ||
| <Button variant="outline"> | ||
| {collapsibleOpen ? ( | ||
| <ChevronUp className="h-4 w-4" /> | ||
| ) : ( | ||
| <ChevronDown className="h-4 w-4" /> | ||
| )} | ||
| </Button> | ||
| </CollapsibleTrigger> | ||
| </div> | ||
| <CollapsibleContent className="block"> | ||
| <Button | ||
| onClick={() => | ||
| void authClient.signIn.social({ | ||
| provider: "discord", | ||
| callbackURL: `${import.meta.env.VITE_APP_URL}/project2/`, | ||
| }) | ||
| } | ||
| variant="outline" | ||
| > | ||
| <SiDiscord className="mr-2 h-4 w-4" /> | ||
| {logInType === "login" ? "Log in" : "Sign up"} with Discord | ||
| </Button> | ||
| </CollapsibleContent> | ||
| </Collapsible> | ||
| ); | ||
| } | ||
|
|
||
| export { SocialLogIns }; | ||
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,19 @@ | ||
| import { Collapsible as CollapsiblePrimitive } from "@base-ui/react/collapsible" | ||
|
|
||
| function Collapsible({ ...props }: CollapsiblePrimitive.Root.Props) { | ||
| return <CollapsiblePrimitive.Root data-slot="collapsible" {...props} /> | ||
| } | ||
|
|
||
| function CollapsibleTrigger({ ...props }: CollapsiblePrimitive.Trigger.Props) { | ||
| return ( | ||
| <CollapsiblePrimitive.Trigger data-slot="collapsible-trigger" {...props} /> | ||
| ) | ||
| } | ||
|
|
||
| function CollapsibleContent({ ...props }: CollapsiblePrimitive.Panel.Props) { | ||
| return ( | ||
| <CollapsiblePrimitive.Panel data-slot="collapsible-content" {...props} /> | ||
| ) | ||
| } | ||
|
|
||
| export { Collapsible, CollapsibleTrigger, CollapsibleContent } |
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,177 @@ | ||
| import { useState } from "react"; | ||
| import { Link, useNavigate } from "react-router"; | ||
| import { useForm } from "@tanstack/react-form"; | ||
| import { z } from "zod"; | ||
| import { authClient } from "~/api/auth"; | ||
| import { Button } from "~/components/ui/Button"; | ||
| import { | ||
| Card, | ||
| CardContent, | ||
| CardDescription, | ||
| CardFooter, | ||
| CardHeader, | ||
| CardTitle, | ||
| } from "~/components/ui/Card"; | ||
| import { | ||
| Field, | ||
| FieldError, | ||
| FieldGroup, | ||
| FieldLabel, | ||
| } from "~/components/ui/Field"; | ||
| import { Input } from "~/components/ui/Input"; | ||
| import { Spinner } from "~/components/ui/Spinner"; | ||
| import { toast } from "~/components/ui/Toast"; | ||
| import { SiGithub } from "@icons-pack/react-simple-icons"; | ||
| import { SocialLogIns } from "~/components/SocialLogIns/SocialLogIns"; | ||
|
|
||
| const formSchema = z.object({ | ||
| email: z.email("Enter a valid email address."), | ||
| password: z.string().min(1, "Enter your password."), | ||
| }); | ||
|
|
||
| export function meta() { | ||
| return [{ title: "Log in | Readr" }]; | ||
| } | ||
|
|
||
| export default function LoginPage() { | ||
| const navigate = useNavigate(); | ||
| const [serverError, setServerError] = useState<string | null>(null); | ||
|
|
||
| const form = useForm({ | ||
| defaultValues: { | ||
| email: "", | ||
| password: "", | ||
| }, | ||
| validators: { | ||
| onSubmit: formSchema, | ||
| }, | ||
| onSubmit: async ({ value }) => { | ||
| setServerError(null); | ||
| const { error, data } = await authClient.signIn.email(value); | ||
|
|
||
| if (error) { | ||
| setServerError(error.message ?? "Could not log in. Please try again."); | ||
| return; | ||
| } | ||
|
|
||
| toast.add({ | ||
| description: `Welcome, ${data.user.name}! You've signed in successfully.`, | ||
| type: "success", | ||
| }); | ||
|
|
||
| await navigate("/"); | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <main className="grid place-items-center px-4 py-20"> | ||
| <Card className="w-full max-w-sm"> | ||
| <CardHeader> | ||
| <CardTitle> | ||
| <h1 className="text-xl">Log in</h1> | ||
| </CardTitle> | ||
| <CardDescription> | ||
| Enter your email and password to log in. | ||
| </CardDescription> | ||
| </CardHeader> | ||
| <CardContent> | ||
| <form | ||
| id="login-form" | ||
| noValidate | ||
| onSubmit={(e) => { | ||
| e.preventDefault(); | ||
| void form.handleSubmit(); | ||
| }} | ||
| > | ||
| <FieldGroup> | ||
| <form.Field name="email"> | ||
| {(field) => { | ||
| const isInvalid = | ||
| field.state.meta.isTouched && !field.state.meta.isValid; | ||
| return ( | ||
| <Field data-invalid={isInvalid}> | ||
| <FieldLabel htmlFor={field.name}>Email</FieldLabel> | ||
| <Input | ||
| id={field.name} | ||
| name={field.name} | ||
| type="email" | ||
| value={field.state.value} | ||
| onBlur={field.handleBlur} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| aria-invalid={isInvalid} | ||
| autoComplete="email" | ||
| /> | ||
| {isInvalid && ( | ||
| <FieldError errors={field.state.meta.errors} /> | ||
| )} | ||
| </Field> | ||
| ); | ||
| }} | ||
| </form.Field> | ||
| <form.Field name="password"> | ||
| {(field) => { | ||
| const isInvalid = | ||
| field.state.meta.isTouched && !field.state.meta.isValid; | ||
| return ( | ||
| <Field data-invalid={isInvalid}> | ||
| <FieldLabel htmlFor={field.name}>Password</FieldLabel> | ||
| <Input | ||
| id={field.name} | ||
| name={field.name} | ||
| type="password" | ||
| value={field.state.value} | ||
| onBlur={field.handleBlur} | ||
| onChange={(e) => field.handleChange(e.target.value)} | ||
| aria-invalid={isInvalid} | ||
| autoComplete="current-password" | ||
| /> | ||
| {isInvalid && ( | ||
| <FieldError errors={field.state.meta.errors} /> | ||
| )} | ||
| </Field> | ||
| ); | ||
| }} | ||
| </form.Field> | ||
| <FieldError>{serverError}</FieldError> | ||
| </FieldGroup> | ||
| </form> | ||
| </CardContent> | ||
| <CardFooter className="flex-col gap-4"> | ||
| <form.Subscribe selector={(state) => state.isSubmitting}> | ||
| {(isSubmitting) => ( | ||
| <Button | ||
| type="submit" | ||
| form="login-form" | ||
| size="lg" | ||
| className="w-full" | ||
| disabled={isSubmitting} | ||
| > | ||
| {isSubmitting && <Spinner aria-hidden="true" />} | ||
| Log in | ||
| </Button> | ||
| )} | ||
| </form.Subscribe> | ||
| <Button | ||
| onClick={() => | ||
| void authClient.signIn.social({ | ||
| provider: "github", | ||
| callbackURL: `${import.meta.env.VITE_APP_URL}/project2/`, | ||
| }) | ||
| } | ||
| variant="outline" | ||
| > | ||
| <SiGithub className="mr-2 h-4 w-4" /> | ||
| Log in with GitHub | ||
| </Button> | ||
| <SocialLogIns logInType="login" /> | ||
| <p className="text-muted-foreground"> | ||
| Don't have an account?{" "} | ||
| <Link to="/signup" className="text-primary underline"> | ||
| Sign up | ||
| </Link> | ||
| </p> | ||
| </CardFooter> | ||
| </Card> | ||
| </main> | ||
| ); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.