create-myexam-app 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -15,12 +15,13 @@ console.log('[3] Library System Management')
15
15
  console.log('[4] Parking Management System')
16
16
  console.log('[5] School fee Management System')
17
17
  console.log('[6]Stock Management System')
18
- console.log('[7] yhh ')
18
+ console.log('[7]userController.js')
19
+ console.log('[8] yhh ')
19
20
 
20
21
 
21
22
 
22
23
 
23
- rl.question('Pick (1-7): ', (answer) => {
24
+ rl.question('Pick (1-8): ', (answer) => {
24
25
 
25
26
  const projects = {
26
27
  '1':'crpms',
@@ -29,7 +30,8 @@ rl.question('Pick (1-7): ', (answer) => {
29
30
  '4': 'parking managementt system',
30
31
  '5': 'sfms-app',
31
32
  '6': 'SMS-',
32
- '7':'yhh',
33
+ '7':'user',
34
+ '8':'yhh',
33
35
 
34
36
  }
35
37
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  "name": "create-myexam-app",
4
4
 
5
- "version": "1.0.8",
5
+ "version": "1.0.10",
6
6
 
7
7
  "description": "My exam projects",
8
8
 
@@ -1,19 +1,26 @@
1
1
  import React, { useState } from "react";
2
- import axios from 'axios';
2
+ import {useNavigate} from 'react-router-dom'
3
+ import axios from 'axios'
3
4
 
4
5
  export default function Login() {
5
6
  const [email, setEmail] = useState('');
6
7
  const [password, setPassword] = useState('');
7
8
  const [error, setError] = useState('');
8
-
9
+ const navigate = useNavigate();
10
+
9
11
  const handleLogin = async (e) => {
10
12
  e.preventDefault();
11
13
  try {
12
14
  const res = await axios.post('http://localhost:5000/api/user/login', { email, password });
13
15
  localStorage.setItem("token", res.data.token);
14
- window.location.href = "/dashboard";
16
+ navigate("/dshboard");
17
+ window.location.reload();
15
18
  } catch (error) {
16
- setError("Failed to login. Please check your credentials.");
19
+ if(error.response && error.response.data){
20
+ setError(error.response.data.message);
21
+ }else{
22
+ setError("Failed to login. Please check your details.")
23
+ }
17
24
  }
18
25
  };
19
26
 
@@ -0,0 +1,35 @@
1
+ import User from "../models/User.js";
2
+ import bcrypt from 'bcrypt'
3
+ import jwt from 'jsonwebtoken'
4
+
5
+
6
+ //register
7
+
8
+ export const registerUser = async(req,res) => {
9
+ try{
10
+ const {username, password} = req.body;
11
+ const hashedPassword = await bcrypt.hash(password, 10);
12
+ const user = await User.create({username,password:hashedPassword});
13
+ res.status(201).json(user);
14
+ }catch(error){
15
+ res.status(500).json({message:error.message});
16
+ }
17
+ };
18
+ //login
19
+
20
+ export const loginUser = async (req,res) => {
21
+ try{
22
+ const {username, password} = req.body;
23
+ const user = await User.findOne({username});
24
+ if(!user) return res.status(400).json({message:"Invalid credetial.."});
25
+
26
+ const isMatch = await bcrypt.compare(password, user.password);
27
+ if(!isMatch) return res.status(400).json({message:"Invalid password"});
28
+
29
+ const token = jwt.sign({id: user._id}, process.env.JWT_SECRET || "secret", {expiresIn:"4d"});
30
+ res.status(200).json({token, user});
31
+ }catch(error){
32
+ res.status(500).json({message:error.message});
33
+ }
34
+ } ;
35
+