-
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
Showing
19 changed files
with
1,063 additions
and
108 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
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,48 @@ | ||
import { router, useLocalSearchParams } from "expo-router"; | ||
import { useState } from "react"; | ||
import { View } from "react-native"; | ||
import { AuthForm } from "src/components/authForm"; | ||
import { Text } from "~/components/ui/text"; | ||
import { AlertCircle } from "~/lib/icons/AlertCircle"; | ||
import { useLogin } from "../hooks/useLogin"; | ||
|
||
const Login = () => { | ||
const { login, isPending } = useLogin(); | ||
const [error, setError] = useState<string | null>(null); | ||
const { redirect } = useLocalSearchParams<{ redirect?: string }>(); | ||
|
||
return ( | ||
<View className="max-w-md mr-auto ml-auto"> | ||
<AuthForm | ||
onSubmit={async ({ password, username }) => { | ||
const sessionKey = await login({ | ||
userIdentifier: username, | ||
password, | ||
}); | ||
if (!sessionKey) { | ||
setError("Failed to login"); | ||
return; | ||
} | ||
if (redirect) { | ||
router.navigate(redirect); | ||
return; | ||
} | ||
router.navigate("/"); | ||
}} | ||
children={<Text>Login</Text>} | ||
isPending={isPending} | ||
/> | ||
|
||
{error && ( | ||
<View className="mt-4"> | ||
<AlertCircle className="h-4 w-4" /> | ||
{/* TODO proper styling */} | ||
<Text>Error</Text> | ||
<Text>Failed to log in</Text> | ||
</View> | ||
)} | ||
</View> | ||
); | ||
}; | ||
|
||
export default Login; |
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,47 @@ | ||
import { router, useLocalSearchParams } from "expo-router"; | ||
import { useState } from "react"; | ||
import { View } from "react-native"; | ||
import { Text } from "~/components/ui/text"; | ||
import { AlertCircle } from "~/lib/icons/AlertCircle"; | ||
import { AuthForm } from "../components/authForm"; | ||
import { useRegisterAndLogin } from "../hooks/useRegisterAndLogin"; | ||
|
||
const Register = () => { | ||
const { registerAndLogin, isPending } = useRegisterAndLogin(); | ||
const { redirect } = useLocalSearchParams<{ redirect?: string }>(); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
return ( | ||
<View className="max-w-md mr-auto ml-auto"> | ||
<AuthForm | ||
onSubmit={async ({ password, username }) => { | ||
const sessionKey = await registerAndLogin({ | ||
userIdentifier: username, | ||
password, | ||
}); | ||
if (!sessionKey) { | ||
setError("Failed to register"); | ||
return; | ||
} | ||
if (redirect) { | ||
router.navigate(redirect); | ||
return; | ||
} | ||
router.navigate("/"); | ||
}} | ||
children={<Text>Register</Text>} | ||
isPending={isPending} | ||
/> | ||
{error && ( | ||
<View className="mt-4"> | ||
<AlertCircle className="h-4 w-4" /> | ||
{/* TODO proper styling */} | ||
<Text>Error</Text> | ||
<Text>Failed to register</Text> | ||
</View> | ||
)} | ||
</View> | ||
); | ||
}; | ||
|
||
export default Register; |
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,57 @@ | ||
import { useState } from "react"; | ||
import { View } from "react-native"; | ||
import { Button } from "~/components/ui/button"; | ||
import { Input } from "~/components/ui/input"; | ||
import { Text } from "~/components/ui/text"; | ||
|
||
type Props = { | ||
onSubmit: (params: { username: string; password: string }) => void; | ||
isPending: boolean; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export const AuthForm = ({ onSubmit, isPending, children }: Props) => { | ||
const [username, setUsername] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
|
||
return ( | ||
<View> | ||
<Text className="text-xl text-center font-semibold mb-8 mt-12"> | ||
{children} | ||
</Text> | ||
|
||
<View className="gap-4 flex flex-col"> | ||
<Input | ||
// required TODO | ||
className="border border-slate-300 p-2 rounded" | ||
placeholder="Username" | ||
autoComplete="off" | ||
value={username} | ||
onChangeText={(value) => { | ||
setUsername(value); | ||
}} | ||
/> | ||
|
||
<Input | ||
// required | ||
className="border border-slate-300 p-2 rounded" | ||
placeholder="Password" | ||
autoComplete="off" | ||
value={password} | ||
onChangeText={(value) => { | ||
setPassword(value); | ||
}} | ||
/> | ||
|
||
<Button | ||
disabled={isPending} | ||
onPress={() => { | ||
onSubmit({ username, password }); | ||
}} | ||
> | ||
{children} | ||
</Button> | ||
</View> | ||
</View> | ||
); | ||
}; |
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,24 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
type IntervalFunc = () => unknown | void; | ||
|
||
// Inspired by https://overreacted.io/making-setinterval-declarative-with-react-hooks/ | ||
export const useInterval = (callback: IntervalFunc, delay: number | null) => { | ||
const savedCallback = useRef<IntervalFunc | null>(null); | ||
|
||
useEffect(() => { | ||
if (delay === null) return; | ||
savedCallback.current = callback; | ||
}); | ||
|
||
useEffect(() => { | ||
if (delay === null) return; | ||
function tick() { | ||
if (savedCallback.current !== null) { | ||
savedCallback.current(); | ||
} | ||
} | ||
const id = setInterval(tick, delay); | ||
return () => clearInterval(id); | ||
}, [delay]); | ||
}; |
Oops, something went wrong.