Skip to content

Instantly share code, notes, and snippets.

@kuc-arc-f
Last active March 27, 2022 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuc-arc-f/076db83ad05d1e6a8a32451c3d6e7769 to your computer and use it in GitHub Desktop.
Save kuc-arc-f/076db83ad05d1e6a8a32451c3d6e7769 to your computer and use it in GitHub Desktop.
firebase v9 auth, SignUp sample
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth'
const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGE_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_SENDER_ID,
};
initializeApp(firebaseConfig);
export const auth = getAuth();
import React from 'react';
//import { useState } from 'react';
import {auth} from '../../firebase';
//import { createUserWithEmailAndPassword } from 'firebase/auth'
//import { getAuth, signInWithEmailAndPassword } from 'firebase/auth'
import { signInWithEmailAndPassword } from 'firebase/auth'
import { Link } from 'react-router-dom';
const Login = () => {
const handleSubmit = (event: any) => {
event.preventDefault();
const { email, password } = event.target.elements;
signInWithEmailAndPassword(auth, email.value, password.value)
.then((user) => {
console.log('ログイン成功=', user.user.uid)
})
.catch((error) => {
console.error(error)
})
};
return (
<div>
<h1>ログイン</h1>
<form onSubmit={handleSubmit}>
<div>
<label>メールアドレス</label>
<input name="email" type="email" placeholder="email" />
</div>
<div>
<label>パスワード</label>
<input name="password" type="password" placeholder="password" />
</div>
<hr />
<div>
<button>ログイン</button>
</div>
<hr />
<div>
<Link to={'/signup'}><button>Register</button>
</Link>
</div>
</form>
</div>
);
};
export default Login
import React from 'react';
import { useState } from 'react';
import {auth} from '../../firebase';
import { createUserWithEmailAndPassword } from 'firebase/auth'
const SignUp = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
console.log(email, password);
const handleSubmit = (event: any) => {
event.preventDefault();
const { email, password } = event.target.elements;
createUserWithEmailAndPassword(auth, email.value, password.value)
.then(( userCredential) => {
console.log('user created');
console.log(userCredential)
})
.catch((error) => {
alert(error.message)
console.error(error)
});
console.log(email.value);
};
const handleChangeEmail = (event: any) => {
setEmail(event.currentTarget.value);
};
const handleChangePassword = (event: any) => {
setPassword(event.currentTarget.value);
};
return (
<div>
<h1>ユーザ登録</h1>
<form onSubmit={handleSubmit}>
<div>
<label>メールアドレス</label>
<input
name="email"
type="email"
placeholder="email"
onChange={(event) => handleChangeEmail(event)}
/>
</div>
<div>
<label>パスワード</label>
<input
name="password"
type="password"
placeholder="password"
onChange={(event) => handleChangePassword(event)}
/>
</div>
<hr />
<div>
<button>登録</button>
</div>
</form>
</div>
);
};
export default SignUp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment