Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
R24-152
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
19
Issues
19
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tharaka it19975696
R24-152
Commits
6ca90fb0
Commit
6ca90fb0
authored
Apr 13, 2024
by
Tharaka it19975696
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed login page link issue
parent
186b4e79
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
34 deletions
+41
-34
server/index.js
server/index.js
+6
-8
src/components/App.js
src/components/App.js
+12
-4
src/components/LoginPage.js
src/components/LoginPage.js
+1
-1
src/components/RegisterPage.js
src/components/RegisterPage.js
+22
-21
No files found.
server/index.js
View file @
6ca90fb0
...
...
@@ -22,14 +22,10 @@ if (!JWT_SECRET) {
process
.
exit
(
1
);
}
// Helper function to create a JWT token
const
createToken
=
(
userId
,
role
)
=>
{
return
jwt
.
sign
(
{
userId
,
role
},
JWT_SECRET
,
{
expiresIn
:
'
1h
'
}
);
};
// Utility function to create JWT token
const
createToken
=
(
userId
,
userType
)
=>
{
return
jwt
.
sign
({
userId
,
userType
},
JWT_SECRET
,
{
expiresIn
:
'
1h
'
});
};
// Registration endpoint for a student
app
.
post
(
'
/register/student
'
,
async
(
req
,
res
)
=>
{
...
...
@@ -46,6 +42,7 @@ app.post('/register/student', async (req, res) => {
data
:
{
username
,
password
:
hashedPassword
}
});
//Token Generation
const
token
=
createToken
(
student
.
id
,
'
student
'
);
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
}
catch
(
error
)
{
...
...
@@ -68,6 +65,7 @@ app.post('/register/student', async (req, res) => {
data
:
{
username
,
password
:
hashedPassword
}
});
//Token Generation
const
token
=
createToken
(
teacher
.
id
,
'
teacher
'
);
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
}
catch
(
error
)
{
...
...
src/components/App.js
View file @
6ca90fb0
...
...
@@ -20,6 +20,12 @@ function App() {
setIsLoggedIn
(
!!
token
);
},
[]);
// Function to call when the registration is successful
const
onRegistrationSuccess
=
(
token
)
=>
{
localStorage
.
setItem
(
'
token
'
,
token
);
// Save the token to localStorage
setIsLoggedIn
(
true
);
// Update the login state to true
};
const
handleLogout
=
()
=>
{
localStorage
.
removeItem
(
'
token
'
);
setIsLoggedIn
(
false
);
...
...
@@ -29,9 +35,11 @@ function App() {
setIsLoggedIn
(
true
);
};
const
handleRegistrationSuccess
=
()
=>
{
setIsLoggedIn
(
true
);
};
// const handleRegistrationSuccess = () => {
// setIsLoggedIn(true);
// };
return
(
<
Router
>
...
...
@@ -46,7 +54,7 @@ function App() {
}
/
>
<
Route
path
=
"
/register
"
element
=
{
!
isLoggedIn
?
(
<
RegisterPage
onRegistrationSuccess
=
{
handle
RegistrationSuccess
}
/
>
<
RegisterPage
onRegistrationSuccess
=
{
on
RegistrationSuccess
}
/
>
)
:
(
<
Navigate
replace
to
=
"
/home
"
/>
)
...
...
src/components/LoginPage.js
View file @
6ca90fb0
...
...
@@ -80,7 +80,7 @@ function LoginPage({ onLoginSuccess }) {
<
/button
>
<
p
className
=
"
text-center
"
>
Don
'
t have an account yet?{
'
'
}
<Link to="/" className="text-blue-600 hover:text-blue-800 transition duration-200 ease-in-out">
<Link to="/
register
" className="text-blue-600 hover:text-blue-800 transition duration-200 ease-in-out">
Register here
</Link>
</p>
...
...
src/components/RegisterPage.js
View file @
6ca90fb0
...
...
@@ -3,7 +3,7 @@ import React, { useState } from 'react';
import
{
Link
,
useNavigate
}
from
'
react-router-dom
'
;
import
{
registerStudent
,
registerTeacher
}
from
'
../api/api
'
;
// Assuming these are API calls returning promises
const
RegisterPage
=
()
=>
{
const
RegisterPage
=
(
onRegistrationSuccess
)
=>
{
const
[
userData
,
setUserData
]
=
useState
({
username
:
''
,
password
:
''
,
...
...
@@ -15,32 +15,33 @@ const RegisterPage = () => {
setUserData
({
...
userData
,
[
e
.
target
.
name
]:
e
.
target
.
value
});
};
//Token generation in Registration success
const
handleSubmit
=
async
(
e
)
=>
{
e
.
preventDefault
();
try
{
let
token
;
if
(
userData
.
role
===
'
student
'
)
{
const
response
=
await
registerStudent
({
username
:
userData
.
username
,
password
:
userData
.
password
});
token
=
response
.
token
;
}
else
{
const
response
=
await
registerTeacher
({
username
:
userData
.
username
,
password
:
userData
.
password
});
token
=
response
.
token
;
}
const
response
=
userData
.
role
===
'
student
'
?
await
registerStudent
(
userData
)
:
await
registerTeacher
(
userData
);
if
(
token
)
{
console
.
log
(
"
Token received:
"
,
token
);
// Log for debugging
localStorage
.
setItem
(
'
token
'
,
token
);
// Store the token
if
(
response
.
token
)
{
console
.
log
(
"
Token received:
"
,
response
.
token
);
// Log for debugging
localStorage
.
setItem
(
'
token
'
,
response
.
token
);
// Store the token
if
(
onRegistrationSuccess
)
onRegistrationSuccess
(
response
.
token
);
// Update app state
navigate
(
'
/
'
);
// Redirect to the homepage
}
else
{
console
.
error
(
"
No token received
"
);
// This error message can be refined based on the response structure
alert
(
'
Registration successful but no token received
'
);
}
}
catch
(
error
)
{
console
.
error
(
"
Registration failed:
"
,
error
);
alert
(
error
.
message
);
// This error handling can be refined based on the response structure
alert
(
'
Registration failed:
'
+
(
error
.
response
?.
data
?.
message
||
error
.
message
));
}
};
return
(
<
div
className
=
"
flex items-center justify-center h-screen bg-gray-200
"
>
<
div
className
=
"
bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 flex flex-col
"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment