-
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
5 changed files
with
169 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Head from 'next/head'; | ||
import Login from '@/components/LoginForm'; | ||
|
||
export default function LoginPage() { | ||
return ( | ||
<> | ||
<Head> | ||
<title>Login</title> | ||
</Head> | ||
<h1>Login</h1> | ||
<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
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,73 @@ | ||
"use client"; | ||
import api from "@/api"; | ||
import { useState } from "react"; | ||
import Router, { useRouter } from "next/navigation"; | ||
import Cookies from "js-cookie"; | ||
import { RouterContext } from "next/dist/shared/lib/router-context"; | ||
|
||
interface TokenProps { | ||
token: string; | ||
} | ||
|
||
async function fetchData(username: string, password: string): Promise<TokenProps> { | ||
try { | ||
const response = await api.post('api-token-auth/', { | ||
"username": username, | ||
"password": password | ||
}); | ||
const data = response.data; | ||
return data; | ||
} catch (error) { | ||
console.error(error); | ||
return {token: ""}; | ||
} | ||
} | ||
|
||
export default function Login() { | ||
const [username, setUsername] = useState(""); | ||
const [password, setPassword] = useState(""); | ||
const router = useRouter(); | ||
|
||
async function handleSubmit(event: React.FormEvent) { | ||
event.preventDefault(); | ||
try { | ||
const response = await fetchData(username, password); | ||
if (response.token) { | ||
const token = response.token; | ||
console.log(token); | ||
Cookies.set("token", token, { expires: 1 }); | ||
router.push("/"); | ||
} else { | ||
console.error("Login failed"); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmit} className="text-gray-500"> | ||
<label> | ||
Username: | ||
<input | ||
type="text" | ||
value={username} | ||
onChange={(event) => setUsername(event.target.value)} | ||
className="border-2 border-gray-300" | ||
/> | ||
</label> | ||
<br /> | ||
<label> | ||
Password: | ||
<input | ||
type="password" | ||
value={password} | ||
onChange={(event) => setPassword(event.target.value)} | ||
className="border-2 border-gray-300" | ||
/> | ||
</label> | ||
<br /> | ||
<button type="submit">Login</button> | ||
</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
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