Skip to content

Commit d12396d

Browse files
committed
added firebase auth
1 parent 17b8fae commit d12396d

14 files changed

+6087
-787
lines changed

components/addquote.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useState } from "react"
2+
import { getToken } from "../util/client.firebase-auth"
3+
4+
export const AddQuote = ({ user }) => {
5+
const [quote, setQuote] = useState("")
6+
const [author, setAuthor] = useState("")
7+
return <form className='p-12 bg-pink-50 border border-blue-700 rounded-full text-blue-700 inline-flex flex-col items-end' onSubmit={async (e) => {
8+
e.preventDefault()
9+
const token = await getToken(user)
10+
const fetch = await window.fetch('api/add', {
11+
method: 'POST',
12+
headers: {
13+
'Accept': 'application/json',
14+
'Content-Type': 'application/json'
15+
},
16+
body: JSON.stringify({ author, quote, token })
17+
});
18+
const content = await fetch.json();
19+
}}>
20+
<div>
21+
<label htmlFor='quote'>Quote</label>
22+
<textarea value={quote} onChange={e => setQuote(e.target.value)} id="quote" className="h-32 ml-2" />
23+
</div>
24+
<div>
25+
<label htmlFor='author'>Author</label>
26+
<input value={author} className='my-2 ml-2' onChange={e => setAuthor(e.target.value)} id="author" type='text' />
27+
</div>
28+
<button type='submit'>Add Quote</button>
29+
</form>
30+
}

components/connection.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const Connection = ({ isConnected }) => {
2+
return <>{!isConnected && <h2>You are NOT connected to MongoDB</h2>}</>
3+
}

components/login.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { signIn } from '../util/client.firebase-auth'
2+
import { useState } from 'react'
3+
4+
export const Login = () => {
5+
const [email, setEmail] = useState("")
6+
const [password, setPassword] = useState("")
7+
const [message, setMessage] = useState("")
8+
return <>
9+
<form className='p-7 bg-pink-50 border border-blue-700 rounded-full text-blue-700 inline-flex flex-col items-end' onSubmit={async (e) => {
10+
e.preventDefault()
11+
const response = await signIn(email, password)
12+
if (response.email) {
13+
setMessage(`Logged in as ${response.email}`)
14+
} else {
15+
setMessage(response)
16+
}
17+
}}>
18+
<div className='inline-block'>
19+
<label htmlFor='email' >E-mail</label>
20+
<input className='my-2 ml-2' value={email} onChange={e => setEmail(e.target.value)} id="email" type="email"></input>
21+
</div>
22+
<div className='inline-block'>
23+
<label htmlFor='password' >Password</label>
24+
<input className='my-2 ml-2' value={password} onChange={e => setPassword(e.target.value)} id="password" type="password"></input>
25+
</div>
26+
<button type='submit'>Login</button>
27+
{message && <div className='not-italic font-bold'><em>{message}</em></div>}
28+
</form>
29+
30+
</>
31+
}

components/loginform.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Login } from './login'
2+
import { Logout } from './logout'
3+
4+
export const LoginForm = ({ user }) => {
5+
return <>
6+
{!user && <Login />}
7+
{user && <Logout />}
8+
</>
9+
}

components/logout.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { signOut } from "../util/client.firebase-auth"
2+
3+
export const Logout = () => {
4+
return <button className="bg-purple-300 rounded-full m-4 p-4" onClick={async () => {
5+
await signOut()
6+
}}>Logout</button>
7+
}

components/navigation.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import styles from '../styles/navigation.module.scss'
22
import Link from 'next/link'
33

44
const registerUser = event => {
5-
event.preventDefault()
5+
event.preventDefault()
66
}
77

88
export default function Search() {
9-
//export default function Search({updateSearch}) {
10-
return (
11-
<nav id={styles.navigation}>
12-
<ul>
13-
<Link href="/"><a><li>Home</li></a></Link>
14-
<Link href="/authors"><a><li>Authors</li></a></Link>
15-
<Link href="/random"><a><li>Random</li></a></Link>
16-
<Link href="/add"><a><li>Add</li></a></Link>
17-
<li><form onSubmit={registerUser}>
9+
//export default function Search({updateSearch}) {
10+
return (
11+
<nav id={styles.navigation}>
12+
<ul>
13+
<Link href="/"><a><li>Home</li></a></Link>
14+
<Link href="/authors"><a><li>Authors</li></a></Link>
15+
<Link href="/random"><a><li>Random</li></a></Link>
16+
<Link href="/add"><a><li>Add</li></a></Link>
17+
{/* <li><form onSubmit={registerUser}>
1818
<label htmlFor="name">Name</label>
1919
<input id="name" type="text" onChange={function(event) {
2020
updateSearch(event.target.value)
@@ -25,8 +25,8 @@ export default function Search() {
2525
<option value="author">Author</option>
2626
</select>
2727
<button type="submit">Search</button>
28-
</form></li>
29-
</ul>
30-
</nav>
31-
)
28+
</form></li> */}
29+
</ul>
30+
</nav>
31+
)
3232
}

0 commit comments

Comments
 (0)