PROGRAMING/FULL STACK

[Auth] Build create account page

donghunl 2024. 3. 26. 09:04
반응형

계정이 없는 사용자를 위해 사용자를 만들수 있는 계정 생성 페이지를 만들겠습니다.

src폴더안의 pages폴더에 CreatAccountPage.js파일을 생성합니다. 이전에 만들었던 LoginPage.js와 비슷하지만 패스워드 재확인 필드만 더 추가해주면 코드는 다음과 같습니다.

CreateAccountPage.js

import { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { getAuth, createUserWithEmailAndPassword } from 'firebase/auth';

const CreateAccountPage = () => {
    const [email, setEmail] = useState('');
    const [password, setPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    const [error, setError] = useState('');

    const navigate = useNavigate();

    const createAccount = async () => {
        try {
            if (password !== confirmPassword) {
                setError('Password and confirm password do not match');
                return;
            }

            await createUserWithEmailAndPassword(getAuth(), email, password);
            navigate('/articles');
        } catch (e) {
            setError(e.message);
        }
    }

    return (
        <>
            <h1>Create Account</h1>
            {error && <p className="error">{error}</p>}
            <input
                placeholder="Your email address"
                value={email}
                onChange={e => setEmail(e.target.value)} />
            <input
                type="password"
                placeholder="Your password"
                value={password}
                onChange={e => setPassword(e.target.value)} />
            <input
                type="password"
                placeholder="Re-enter your password"
                value={confirmPassword}
                onChange={e => setConfirmPassword(e.target.value)} />
            <button onClick={createAccount}>Create Account</button>
            <Link to="/login">Already have an account? Log in here</Link>
        </>
    );

}

export default CreateAccountPage;

그리고 페이지 경로설정을 위하여 App.js파일도 변경합니다.

App.js

import './App.css';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import NavBar from './NavBar';
import HomePage from './pages/HomePage';
import AboutPage from './pages/AboutPage';
import ArticlesListPage from './pages/ArticlesListPage';
import ArticlePage from './pages/ArticlePage';
import NotFoundPage from './pages/NotFoundPage';
import LoginPage from './pages/LoginPage';
import CreateAccountPage from './pages/CreateAccountPage';

function App() {
  return (
    <BrowserRouter>
      <div className="App">
        <NavBar />
        <div id="page-body">
          <Routes>
            <Route path="/" element={<HomePage />} />
            <Route path="/about" element={<AboutPage />} />
            <Route path="/articles" element={<ArticlesListPage />} />
            <Route path="/articles/:articleId" element={<ArticlePage />} />
            <Route path="/login" element={<LoginPage />} />
            <Route path="/create-account" element={<CreateAccountPage />} />
            <Route path="*" element={<NotFoundPage />} />
          </Routes>
        </div>
      </div>
    </BrowserRouter>
  );
}

export default App;

결과 화면은 다음과 같습니다.

반응형

'PROGRAMING > FULL STACK' 카테고리의 다른 글

[Auth] Protecting endpoints using auth-tokens  (0) 2024.03.26
[Auth] Adding Firebase Auth to Node.js  (0) 2024.03.26
[Auth] Custom auth hook  (0) 2024.03.23
[Auth] Login form  (0) 2024.03.23
[Auth] Adding Firebase Auth to React  (0) 2024.03.23