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
Hide 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) {
...
@@ -22,14 +22,10 @@ if (!JWT_SECRET) {
process
.
exit
(
1
);
process
.
exit
(
1
);
}
}
// Helper function to create a JWT token
// Utility function to create JWT token
const
createToken
=
(
userId
,
role
)
=>
{
const
createToken
=
(
userId
,
userType
)
=>
{
return
jwt
.
sign
(
return
jwt
.
sign
({
userId
,
userType
},
JWT_SECRET
,
{
expiresIn
:
'
1h
'
});
{
userId
,
role
},
};
JWT_SECRET
,
{
expiresIn
:
'
1h
'
}
);
};
// Registration endpoint for a student
// Registration endpoint for a student
app
.
post
(
'
/register/student
'
,
async
(
req
,
res
)
=>
{
app
.
post
(
'
/register/student
'
,
async
(
req
,
res
)
=>
{
...
@@ -46,6 +42,7 @@ app.post('/register/student', async (req, res) => {
...
@@ -46,6 +42,7 @@ app.post('/register/student', async (req, res) => {
data
:
{
username
,
password
:
hashedPassword
}
data
:
{
username
,
password
:
hashedPassword
}
});
});
//Token Generation
const
token
=
createToken
(
student
.
id
,
'
student
'
);
const
token
=
createToken
(
student
.
id
,
'
student
'
);
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
}
catch
(
error
)
{
}
catch
(
error
)
{
...
@@ -68,6 +65,7 @@ app.post('/register/student', async (req, res) => {
...
@@ -68,6 +65,7 @@ app.post('/register/student', async (req, res) => {
data
:
{
username
,
password
:
hashedPassword
}
data
:
{
username
,
password
:
hashedPassword
}
});
});
//Token Generation
const
token
=
createToken
(
teacher
.
id
,
'
teacher
'
);
const
token
=
createToken
(
teacher
.
id
,
'
teacher
'
);
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
res
.
status
(
201
).
json
({
token
});
// Send the token in the response
}
catch
(
error
)
{
}
catch
(
error
)
{
...
...
src/components/App.js
View file @
6ca90fb0
...
@@ -19,6 +19,12 @@ function App() {
...
@@ -19,6 +19,12 @@ function App() {
const
token
=
localStorage
.
getItem
(
'
token
'
);
const
token
=
localStorage
.
getItem
(
'
token
'
);
setIsLoggedIn
(
!!
token
);
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
=
()
=>
{
const
handleLogout
=
()
=>
{
localStorage
.
removeItem
(
'
token
'
);
localStorage
.
removeItem
(
'
token
'
);
...
@@ -29,9 +35,11 @@ function App() {
...
@@ -29,9 +35,11 @@ function App() {
setIsLoggedIn
(
true
);
setIsLoggedIn
(
true
);
};
};
const
handleRegistrationSuccess
=
()
=>
{
// const handleRegistrationSuccess = () => {
setIsLoggedIn
(
true
);
// setIsLoggedIn(true);
};
// };
return
(
return
(
<
Router
>
<
Router
>
...
@@ -46,7 +54,7 @@ function App() {
...
@@ -46,7 +54,7 @@ function App() {
}
/
>
}
/
>
<
Route
path
=
"
/register
"
element
=
{
<
Route
path
=
"
/register
"
element
=
{
!
isLoggedIn
?
(
!
isLoggedIn
?
(
<
RegisterPage
onRegistrationSuccess
=
{
handle
RegistrationSuccess
}
/
>
<
RegisterPage
onRegistrationSuccess
=
{
on
RegistrationSuccess
}
/
>
)
:
(
)
:
(
<
Navigate
replace
to
=
"
/home
"
/>
<
Navigate
replace
to
=
"
/home
"
/>
)
)
...
...
src/components/LoginPage.js
View file @
6ca90fb0
...
@@ -80,7 +80,7 @@ function LoginPage({ onLoginSuccess }) {
...
@@ -80,7 +80,7 @@ function LoginPage({ onLoginSuccess }) {
<
/button
>
<
/button
>
<
p
className
=
"
text-center
"
>
<
p
className
=
"
text-center
"
>
Don
'
t have an account yet?{
'
'
}
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
Register here
</Link>
</Link>
</p>
</p>
...
...
src/components/RegisterPage.js
View file @
6ca90fb0
...
@@ -3,7 +3,7 @@ import React, { useState } from 'react';
...
@@ -3,7 +3,7 @@ import React, { useState } from 'react';
import
{
Link
,
useNavigate
}
from
'
react-router-dom
'
;
import
{
Link
,
useNavigate
}
from
'
react-router-dom
'
;
import
{
registerStudent
,
registerTeacher
}
from
'
../api/api
'
;
// Assuming these are API calls returning promises
import
{
registerStudent
,
registerTeacher
}
from
'
../api/api
'
;
// Assuming these are API calls returning promises
const
RegisterPage
=
()
=>
{
const
RegisterPage
=
(
onRegistrationSuccess
)
=>
{
const
[
userData
,
setUserData
]
=
useState
({
const
[
userData
,
setUserData
]
=
useState
({
username
:
''
,
username
:
''
,
password
:
''
,
password
:
''
,
...
@@ -15,31 +15,32 @@ const RegisterPage = () => {
...
@@ -15,31 +15,32 @@ const RegisterPage = () => {
setUserData
({
...
userData
,
[
e
.
target
.
name
]:
e
.
target
.
value
});
setUserData
({
...
userData
,
[
e
.
target
.
name
]:
e
.
target
.
value
});
};
};
const
handleSubmit
=
async
(
e
)
=>
{
//Token generation in Registration success
e
.
preventDefault
();
try
{
const
handleSubmit
=
async
(
e
)
=>
{
let
token
;
e
.
preventDefault
();
if
(
userData
.
role
===
'
student
'
)
{
try
{
const
response
=
await
registerStudent
({
username
:
userData
.
username
,
password
:
userData
.
password
});
const
response
=
userData
.
role
===
'
student
'
?
token
=
response
.
token
;
await
registerStudent
(
userData
)
:
}
else
{
await
registerTeacher
(
userData
);
const
response
=
await
registerTeacher
({
username
:
userData
.
username
,
password
:
userData
.
password
});
token
=
response
.
token
;
if
(
response
.
token
)
{
}
console
.
log
(
"
Token received:
"
,
response
.
token
);
// Log for debugging
localStorage
.
setItem
(
'
token
'
,
response
.
token
);
// Store the token
if
(
token
)
{
if
(
onRegistrationSuccess
)
onRegistrationSuccess
(
response
.
token
);
// Update app state
console
.
log
(
"
Token received:
"
,
token
);
// Log for debugging
localStorage
.
setItem
(
'
token
'
,
token
);
// Store the token
navigate
(
'
/
'
);
// Redirect to the homepage
navigate
(
'
/
'
);
// Redirect to the homepage
}
else
{
}
else
{
console
.
error
(
"
No token received
"
);
// This error message can be refined based on the response structure
alert
(
'
Registration successful but no token received
'
);
alert
(
'
Registration successful but no token received
'
);
}
}
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"
Registration failed:
"
,
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
(
return
(
<
div
className
=
"
flex items-center justify-center h-screen bg-gray-200
"
>
<
div
className
=
"
flex items-center justify-center h-screen bg-gray-200
"
>
...
...
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