Return user and Token instead of decoding token. Added transform to not store image in redux store
This commit is contained in:
1 parent
440716b021
commit
f97aa1ca8c
4 files changed
+26
-18
No files matched your search
@@ -67,10 +67,8 @@ const handleAuthFulfilled = (state, action) => {
|
|||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.success = action.payload.success;
|
state.success = action.payload.success;
|
||||||
state.msg = action.payload.msg;
|
state.msg = action.payload.msg;
|
||||||
state.authToken = action.payload.data;
|
state.authToken = action.payload.data.token;
|
||||||
const decodedToken = jwtDecode(action.payload.data);
|
state.user = action.payload.data.user;
|
||||||
const user = { ...decodedToken };
|
|
||||||
state.user = user;
|
|
||||||
};
|
};
|
||||||
const handleAuthRejected = (state, action) => {
|
const handleAuthRejected = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
|
|||||||
+13
-1
@@ -2,12 +2,24 @@ import { configureStore, combineReducers } from "@reduxjs/toolkit";
|
|||||||
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
||||||
import authReducer from "./Features/Auth/authSlice";
|
import authReducer from "./Features/Auth/authSlice";
|
||||||
import storage from "redux-persist/lib/storage";
|
import storage from "redux-persist/lib/storage";
|
||||||
import { persistReducer, persistStore } from "redux-persist";
|
import { persistReducer, persistStore, createTransform } from "redux-persist";
|
||||||
|
|
||||||
|
const authTransform = createTransform(
|
||||||
|
(inboundState) => {
|
||||||
|
const { profileImage, ...rest } = inboundState;
|
||||||
|
return rest;
|
||||||
|
},
|
||||||
|
// No transformation on rehydration
|
||||||
|
null,
|
||||||
|
// Only applies to auth
|
||||||
|
{ whitelist: ["auth"] }
|
||||||
|
);
|
||||||
|
|
||||||
const persistConfig = {
|
const persistConfig = {
|
||||||
key: "root",
|
key: "root",
|
||||||
storage,
|
storage,
|
||||||
whitielist: ["auth", "monitors"],
|
whitielist: ["auth", "monitors"],
|
||||||
|
transforms: [authTransform],
|
||||||
};
|
};
|
||||||
|
|
||||||
const rootReducer = combineReducers({
|
const rootReducer = combineReducers({
|
||||||
|
|||||||
@@ -55,7 +55,11 @@ const registerController = async (req, res, next) => {
|
|||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
userId: newUser._id,
|
userId: newUser._id,
|
||||||
});
|
});
|
||||||
const token = issueToken(newUser._doc);
|
|
||||||
|
const userForToken = { ...newUser._doc };
|
||||||
|
delete userForToken.profileImage;
|
||||||
|
|
||||||
|
const token = issueToken(userForToken);
|
||||||
|
|
||||||
// Sending email to user with pre defined template
|
// Sending email to user with pre defined template
|
||||||
const template = registerTemplate("https://www.bluewavelabs.ca");
|
const template = registerTemplate("https://www.bluewavelabs.ca");
|
||||||
@@ -69,7 +73,7 @@ const registerController = async (req, res, next) => {
|
|||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_CREATE_USER,
|
msg: successMessages.AUTH_CREATE_USER,
|
||||||
data: token,
|
data: { user: newUser, token: token },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
@@ -108,7 +112,7 @@ const loginController = async (req, res, next) => {
|
|||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_LOGIN_USER,
|
msg: successMessages.AUTH_LOGIN_USER,
|
||||||
data: token,
|
data: { user: userWithoutPassword, token: token },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.status = 500;
|
error.status = 500;
|
||||||
|
|||||||
+4
-10
@@ -42,9 +42,7 @@ const insertUser = async (req, res) => {
|
|||||||
}
|
}
|
||||||
const newUser = new UserModel(userData);
|
const newUser = new UserModel(userData);
|
||||||
await newUser.save();
|
await newUser.save();
|
||||||
return await UserModel.findOne({ _id: newUser._id })
|
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find
|
||||||
.select("-password")
|
|
||||||
.select("-profileImage"); // .select() doesn't work with create, need to save then find
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === DUPLICATE_KEY_CODE) {
|
if (error.code === DUPLICATE_KEY_CODE) {
|
||||||
throw new Error(errorMessages.DB_USER_EXISTS);
|
throw new Error(errorMessages.DB_USER_EXISTS);
|
||||||
@@ -67,10 +65,8 @@ const insertUser = async (req, res) => {
|
|||||||
const getUserByEmail = async (req, res) => {
|
const getUserByEmail = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// Need the password to be able to compare, removed .select()
|
// Need the password to be able to compare, removed .select()
|
||||||
// PW hash is removed in controller
|
// We can strip the hash before returing the user
|
||||||
const user = await UserModel.findOne({ email: req.body.email }).select(
|
const user = await UserModel.findOne({ email: req.body.email });
|
||||||
"-profileImage"
|
|
||||||
);
|
|
||||||
if (user) {
|
if (user) {
|
||||||
return user;
|
return user;
|
||||||
} else {
|
} else {
|
||||||
@@ -106,9 +102,7 @@ const updateUser = async (req, res) => {
|
|||||||
candidateUserId,
|
candidateUserId,
|
||||||
candidateUser,
|
candidateUser,
|
||||||
{ new: true } // Returns updated user instead of pre-update user
|
{ new: true } // Returns updated user instead of pre-update user
|
||||||
)
|
).select("-password");
|
||||||
.select("-password")
|
|
||||||
.select("-profileImage");
|
|
||||||
return updatedUser;
|
return updatedUser;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in new issue
Block a user