Skip to content

Commit

Permalink
signup function
Browse files Browse the repository at this point in the history
  • Loading branch information
u0509421 committed Jan 11, 2025
1 parent 3b0e45f commit 55ff188
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 52 deletions.
35 changes: 18 additions & 17 deletions frontend/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import js from '@eslint/js'
import globals from 'globals'
import react from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import js from "@eslint/js";
import globals from "globals";
import react from "eslint-plugin-react";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";

export default [
{ ignores: ['dist'] },
{ ignores: ["dist"] },
{
files: ['**/*.{js,jsx}'],
files: ["**/*.{js,jsx}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaVersion: "latest",
ecmaFeatures: { jsx: true },
sourceType: 'module',
sourceType: "module",
},
},
settings: { react: { version: '18.3' } },
settings: { react: { version: "18.3" } },
plugins: {
react,
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
"react-hooks": reactHooks,
"react-refresh": reactRefresh,
},
rules: {
...js.configs.recommended.rules,
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
...react.configs["jsx-runtime"].rules,
...reactHooks.configs.recommended.rules,
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
"react/jsx-no-target-blank": "off",
"react-refresh/only-export-components": [
"warn",
{ allowConstantExport: true },
],
"react/prop-types": "off",
},
},
]
];
98 changes: 96 additions & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.4.0"
"react-hot-toast": "^2.5.1",
"react-icons": "^5.4.0",
"react-router-dom": "^7.1.1"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Routes, Route } from "react-router-dom";
import "./App.css";
import Home from "./pages/home/Home";
import Login from "./pages/login/login";
import SignUp from "./pages/signup/SignUp";
import { Toaster } from "react-hot-toast";

function App() {
return (
<div className="p-4 h-screen flex justify-center items-center">
<Home />
{/* <SignUp /> */}
<Routes>
<Route path="/" element={<Home />} />
<Route path="/login" element={<Login />} />
<Route path="/signup" element={<SignUp />} />
</Routes>
<Toaster />
</div>
);
}
Expand Down
72 changes: 72 additions & 0 deletions frontend/src/hooks/useSignup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { useState } from "react";
import toast from "react-hot-toast";

const useSignup = () => {
const [loading, setLoading] = useState(false);

const signup = async ({
fullName,
username,
password,
confirmPassword,
gender,
}) => {
const success = handleInputErrors({
fullName,
username,
password,
confirmPassword,
gender,
});
if (!success) return;

setLoading(true);

try {
const res = await fetch("/api/auth/signup", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
fullName,
username,
password,
confirmPassword,
gender,
}),
});
const data = await res.json();
console.log(data);
} catch (error) {
toast.error(error.message);
} finally {
setLoading(false);
}
};
return { loading, signup };
};

export default useSignup;

function handleInputErrors({
fullName,
username,
password,
confirmPassword,
gender,
}) {
if (!fullName || !username || !password || !confirmPassword || !gender) {
toast.error("All fields are required");
return false;
}
if (password !== confirmPassword) {
toast.error("Passwords do not match");
return false;
}
if (password.length < 6) {
toast.error("Password must be at least 6 characters");
return false;
}
return true;
}
21 changes: 12 additions & 9 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import React from "react";
import ReactDom from "react-dom/client";
import "./index.css";
import App from "./App.jsx";
import { BrowserRouter } from "react-router-dom";

createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>,
)
ReactDom.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
8 changes: 4 additions & 4 deletions frontend/src/pages/login/login.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import { Link } from "react-router-dom";

const login = () => {
return (
Expand Down Expand Up @@ -29,12 +29,12 @@ const login = () => {
className="w-full input input-bordered h-10"
/>
</div>
<a
href="#"
<Link
to="/signup"
className="text-sm hover:underline hover:text-blue-600 mt-2 inline-block"
>
Don&apos;t have an account? Signup
</a>
</Link>
<div>
<button className="btn btn-block btn-sm mt-2">Login</button>
</div>
Expand Down
Loading

0 comments on commit 55ff188

Please sign in to comment.