remove result of user creation
This commit is contained in:
@@ -26,12 +26,15 @@ export const AuthProvider = ({ children, ...rest }: AuthProviderProps) => {
|
||||
const [loading] = useState(true);
|
||||
const [isLoginDialogOpen, setIsLoginDialogOpen] = useState(false);
|
||||
const token = cookies.token;
|
||||
const setToken = (value: string) => {
|
||||
const setToken = useCallback(
|
||||
(value: string) => {
|
||||
setCookie("token", value);
|
||||
};
|
||||
const removeToken = () => {
|
||||
},
|
||||
[setCookie]
|
||||
);
|
||||
const removeToken = useCallback(() => {
|
||||
removeCookie("token");
|
||||
};
|
||||
}, [removeCookie]);
|
||||
const apiClient = createApiClient(token ?? "");
|
||||
|
||||
const login = useCallback<LoginCallback>(
|
||||
@@ -44,6 +47,7 @@ export const AuthProvider = ({ children, ...rest }: AuthProviderProps) => {
|
||||
}),
|
||||
}).then(async (res) => {
|
||||
setToken(await res.json());
|
||||
setIsLoginDialogOpen(false);
|
||||
});
|
||||
},
|
||||
[apiClient, setToken]
|
||||
@@ -55,7 +59,7 @@ export const AuthProvider = ({ children, ...rest }: AuthProviderProps) => {
|
||||
}).then(() => {
|
||||
removeToken();
|
||||
});
|
||||
}, [apiClient, setToken]);
|
||||
}, [apiClient, removeToken]);
|
||||
|
||||
const authenticated = !!token;
|
||||
|
||||
|
||||
@@ -1,67 +1,63 @@
|
||||
import { Router } from 'express';
|
||||
import { UserDto } from '../dto/user.dto';
|
||||
import { AuthService } from '../service/auth.service';
|
||||
import { UserService } from '../service/user.service';
|
||||
import { Router } from "express";
|
||||
import { AuthService } from "../service/auth.service";
|
||||
import { UserService } from "../service/user.service";
|
||||
|
||||
const router = Router();
|
||||
export const UserRoutes = router;
|
||||
|
||||
export const apiNamespace = '/users'
|
||||
export const apiNamespace = "/users";
|
||||
|
||||
router.post(apiNamespace, (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
|
||||
UserService.create({
|
||||
email,
|
||||
password
|
||||
}).then(user => {
|
||||
const respose: UserDto = {
|
||||
id: user.id,
|
||||
email: user.email
|
||||
}
|
||||
|
||||
res.json(respose);
|
||||
}).catch(err => {
|
||||
res.status(422).json({
|
||||
error: err.message
|
||||
});
|
||||
password,
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
res.json();
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(422).json({
|
||||
error: err.message,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
router.post(`${apiNamespace}/sign_in`, async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
|
||||
const user = await UserService.findByEmail(email)
|
||||
const user = await UserService.findByEmail(email);
|
||||
|
||||
const isPasswordValid = await AuthService.isUserPasswordValid(user, password)
|
||||
const isPasswordValid = await AuthService.isUserPasswordValid(user, password);
|
||||
|
||||
if (isPasswordValid) {
|
||||
const token = await AuthService.createSession(user)
|
||||
const token = await AuthService.createSession(user);
|
||||
|
||||
res.json({
|
||||
auth: true,
|
||||
token
|
||||
})
|
||||
token,
|
||||
});
|
||||
} else {
|
||||
res.status(500).json({
|
||||
auth: false,
|
||||
token: null,
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
router.delete(`${apiNamespace}/sign_out`, async (req, res) => {
|
||||
const token = req.headers['x-access-token']
|
||||
const token = req.headers["x-access-token"];
|
||||
|
||||
if (typeof token === 'string') {
|
||||
await AuthService.destoySession(token)
|
||||
if (typeof token === "string") {
|
||||
await AuthService.destoySession(token);
|
||||
|
||||
res.status(204).json({
|
||||
success: true
|
||||
})
|
||||
success: true,
|
||||
});
|
||||
} else {
|
||||
res.status(422).json({
|
||||
success: false
|
||||
})
|
||||
success: false,
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user