Skip to content

Commit

Permalink
redirect to homepage after register
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Lal committed Nov 26, 2023
1 parent ad9a929 commit 1fecaf2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
12 changes: 10 additions & 2 deletions api/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ exports.userRegisterDetails = async (req, res) => {
$unset: { jwtRegisterToken: 1 }
});

const loginToken = user.getLoginToken();
await user.save();

return res.status(200).json({
success: true,
msg: "Your account is registered successfully!"
msg: "Your account is registered successfully!",
loginToken
});
}
user = await User.findByIdAndUpdate(req.user._id, {
Expand All @@ -179,9 +183,13 @@ exports.userRegisterDetails = async (req, res) => {
$unset: { jwtRegisterToken: 1 }
});

const loginToken = user.getLoginToken();
await user.save();

res.status(200).json({
success: true,
msg: "Your account is registered successfully!"
msg: "Your account is registered successfully!",
loginToken
});
}
catch (error) {
Expand Down
9 changes: 3 additions & 6 deletions api/controllers/socialController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ exports.googleCallbackController = async function (req, res, next) {

if (user.authType === 'register') {
const registerToken = user.getRegisterToken();
const loginToken = user.getLoginToken();
await user.save();

return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}&loginToken=${loginToken}`);
return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}`);
}
else if (user.authType === 'login') {
const loginToken = user.getLoginToken();
Expand Down Expand Up @@ -60,10 +59,9 @@ exports.twitterCallbackController = async function (req, res, next) {

if (user.authType === 'register') {
const registerToken = user.getRegisterToken();
const loginToken = user.getLoginToken();
await user.save();

return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}&loginToken=${loginToken}`);
return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}`);
}
else if (user.authType === 'login') {
const loginToken = user.getLoginToken();
Expand Down Expand Up @@ -128,9 +126,8 @@ exports.linkedinCallbackController = async function (req, res) {
}

const registerToken = user.getRegisterToken();
const loginToken = user.getLoginToken();
await user.save();
return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}&loginToken=${loginToken}`);
return res.redirect(`${process.env.FRONTEND_URL}/auth?type=${user.authType}&response=${encodeURIComponent(JSON.stringify(userData))}&registerToken=${registerToken}`);
}

user = await User.findByIdAndUpdate(user._id, {
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ const OTPVerify = () => {
// User detail component
const NewUser = () => {
const navigate = useNavigate();
const { verifySuccess, user, registerDetail } = useAuthStore()
const { user, registerDetail } = useAuthStore()
const { userRegisterDetails } = useRegisterStore()

const fileRef = useRef()
Expand Down Expand Up @@ -369,7 +369,7 @@ const NewUser = () => {
lastName,
password,
...(imageFile ? { imageFile: imageFile } : { imageFile: '' }),
...(verifySuccess && user ? { shallRedirect: true } : { shallRedirect: false }),
shallRedirect: true,
navigate
})
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/store/useAuthStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const useRegisterStore = create(() => ({
.then((result) => {
if (result.success === true) {
toast.success(result.msg, { duration: 7500 });
useAuthStore.getState().setAuthType("sign-in");
localStorage.setItem("login_token", result.loginToken);
if (shallRedirect) navigate("/");
}
if (result.success === false) toast.error(result.msg, { duration: 7500 });
Expand Down

0 comments on commit 1fecaf2

Please sign in to comment.