fixing auth check and content type error
This commit is contained in:
@@ -22,7 +22,7 @@ export const authCheck = async (
|
||||
// verify jwt token with our api secret
|
||||
var decoded: JwtClaims = jwt.verify(authorization, config.JWT_TOKEN_SECRET);
|
||||
|
||||
res.locals.jwtClaims = decoded;
|
||||
res.locals.userInfo = decoded;
|
||||
|
||||
next();
|
||||
} catch (error) {
|
||||
|
||||
+28
-10
@@ -1,13 +1,14 @@
|
||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
||||
import { JwtClaims, authCheck } from "../middleware/auth";
|
||||
import express, { Application, Request, Response } from "express";
|
||||
|
||||
import LoginResponse from "../../core/responses/login.response";
|
||||
import { OAuth2Client } from "google-auth-library";
|
||||
import { ObjectID } from "bson";
|
||||
import { ObjectId } from "mongodb";
|
||||
import UserDetailsResponse from "../../core/responses/userDetails.response";
|
||||
import { UserService } from "../services/user.service";
|
||||
import { UserStatus } from "../../core/models/user.model";
|
||||
import { authCheck } from "../middleware/auth";
|
||||
import { collections } from "../services/database.service";
|
||||
import { loadConfig } from "../config";
|
||||
|
||||
@@ -23,7 +24,7 @@ export default function getUserRoutes() {
|
||||
router.use(express.json());
|
||||
|
||||
// get user details based on id token
|
||||
// router.get("/", authCheck, getUserDetails);
|
||||
router.get("/", authCheck, getUserDetails);
|
||||
|
||||
router.get("/login", login);
|
||||
|
||||
@@ -34,9 +35,26 @@ export default function getUserRoutes() {
|
||||
|
||||
async function handleAuthCheck(req: Request, res: Response) {
|
||||
try {
|
||||
res.status(200).send();
|
||||
res.status(200).json("You are good to go!");
|
||||
} catch (error) {
|
||||
res.status(401).send();
|
||||
res.status(401).json("Unauthorized");
|
||||
}
|
||||
}
|
||||
|
||||
async function getUserDetails(req: Request, res: Response) {
|
||||
try {
|
||||
console.log(res.locals.userInfo as JwtClaims);
|
||||
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
|
||||
const user = await UserService.getUserById(userInfo.userId);
|
||||
|
||||
user
|
||||
? res.status(200).json(new UserDetailsResponse(user))
|
||||
: res.status(404).json("No such user");
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json(`Problem with signing user up or logging in`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +74,7 @@ async function login(req: Request, res: Response) {
|
||||
const email = ticket.getPayload()?.email as string;
|
||||
|
||||
if (!googleUserId || !email) {
|
||||
res.status(401).send("no google account found");
|
||||
res.status(401).json("no google account found");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -66,7 +84,7 @@ async function login(req: Request, res: Response) {
|
||||
// if no user found, then go ahead and create user
|
||||
if (!user) {
|
||||
if (!access_token) {
|
||||
res.status(400).send("No access token provided");
|
||||
res.status(400).json("No access token provided");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -109,8 +127,8 @@ async function login(req: Request, res: Response) {
|
||||
);
|
||||
|
||||
insertResult
|
||||
? res.status(200).send(new LoginResponse(jwtToken, newUser))
|
||||
: res.status(500).send("Failed to create account, already exists");
|
||||
? res.status(200).json(new LoginResponse(jwtToken, newUser))
|
||||
: res.status(500).json("Failed to create account, already exists");
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -127,9 +145,9 @@ async function login(req: Request, res: Response) {
|
||||
config.JWT_TOKEN_SECRET
|
||||
);
|
||||
|
||||
res.status(200).send(new LoginResponse(jwtToken, user));
|
||||
res.status(200).json(new LoginResponse(jwtToken, user));
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).send(`Problem with signing user up or logging in`);
|
||||
res.status(500).json(`Problem with signing user up or logging in`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,21 @@ import axios from "axios";
|
||||
import { collections } from "./database.service";
|
||||
|
||||
export class UserService {
|
||||
static async getUserByGoogleId(userId: string) {
|
||||
const query = { googleId: userId };
|
||||
static async getUserById(userId: string) {
|
||||
const query = { _id: userId };
|
||||
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
// exists
|
||||
if (res?._id) {
|
||||
return res as User;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static async getUserByGoogleId(googleUserId: string) {
|
||||
const query = { googleId: googleUserId };
|
||||
|
||||
const res = await collections.users?.findOne(query);
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { User } from "@nirvana/core/models";
|
||||
|
||||
export default class UserDetailsResponse {
|
||||
constructor(public user: User) {}
|
||||
}
|
||||
@@ -1,32 +1,28 @@
|
||||
import NirvanaApi, { authCheck, login } from "./nirvanaApi";
|
||||
import NirvanaApi, { ApiCalls } from "./nirvanaApi";
|
||||
import { useMutation, useQuery } from "react-query";
|
||||
|
||||
// ====== QUERIES
|
||||
export function useAuthCheck() {
|
||||
return useQuery("AUTH_CHECK", authCheck, {
|
||||
return useQuery("AUTH_CHECK", ApiCalls.authCheck, {
|
||||
retry: false,
|
||||
refetchOnWindowFocus: false,
|
||||
enabled: NirvanaApi._jwtToken ? true : false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLogin() {
|
||||
return useMutation("LOGIN", login, {});
|
||||
return useMutation("LOGIN", ApiCalls.login, {});
|
||||
}
|
||||
|
||||
// export function useGetUserDetails() {
|
||||
// const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
// return useQuery(
|
||||
// Querytypes.GET_USER_DETAILS,
|
||||
// () => getUserDetails(authTokens?.accessToken, authTokens?.idToken),
|
||||
// {
|
||||
// retry: false,
|
||||
// refetchOnWindowFocus: false,
|
||||
// onError: (err) => {
|
||||
// console.log(err);
|
||||
// },
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
export function useGetUserDetails() {
|
||||
return useQuery("USER_DETAILS", ApiCalls.getUserDetails, {
|
||||
retry: false,
|
||||
refetchOnWindowFocus: false,
|
||||
onError: (err) => {
|
||||
console.log(err);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// export function useSearch() {
|
||||
// const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
@@ -2,6 +2,7 @@ import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
||||
|
||||
import LoginResponse from "../../../core/responses/login.response";
|
||||
import { User } from "@nirvana/core/models";
|
||||
import UserDetailsResponse from "../../../core/responses/userDetails.response";
|
||||
|
||||
// export const localHost = process.env.REACT_APP_API_DOMAIN;
|
||||
|
||||
@@ -16,9 +17,9 @@ export default class NirvanaApi {
|
||||
// error if no auth token and it's a private route
|
||||
// throw error and show message on anything that is an error from the backend
|
||||
|
||||
try {
|
||||
const fullUrl = localHost + url;
|
||||
const fullUrl = localHost + url;
|
||||
|
||||
try {
|
||||
let res;
|
||||
if (privateRoute && !this._jwtToken)
|
||||
throw Error("No jwt token available!");
|
||||
@@ -33,19 +34,20 @@ export default class NirvanaApi {
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 401) throw Error("You are not authorized here");
|
||||
if (res.status === 401) throw new Error("You are not authorized here");
|
||||
|
||||
throw Error("Something went wrong");
|
||||
throw new Error("Something went wrong");
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function login(reqLoginTokens: {
|
||||
async function login(reqLoginTokens: {
|
||||
accessToken: string;
|
||||
idToken: string;
|
||||
}): Promise<LoginResponse> {
|
||||
@@ -56,6 +58,16 @@ export async function login(reqLoginTokens: {
|
||||
);
|
||||
}
|
||||
|
||||
export async function authCheck(): Promise<void> {
|
||||
async function authCheck(): Promise<void> {
|
||||
return await NirvanaApi.fetch(`/user/authcheck`, "GET", true);
|
||||
}
|
||||
|
||||
async function getUserDetails(): Promise<UserDetailsResponse> {
|
||||
return await NirvanaApi.fetch(`/user`, "GET", true);
|
||||
}
|
||||
|
||||
export const ApiCalls = {
|
||||
login,
|
||||
authCheck,
|
||||
getUserDetails,
|
||||
};
|
||||
|
||||
@@ -1,100 +1,102 @@
|
||||
// import { $authTokens, $searchQuery } from "../../../controller/recoil";
|
||||
// import { Dropdown, Menu } from "antd";
|
||||
// import Logo, { LogoType } from "../../../components/Logo";
|
||||
// import { useRecoilState, useSetRecoilState } from "recoil";
|
||||
import { $authTokens, $searchQuery } from "../../../controller/recoil";
|
||||
import { Dropdown, Menu } from "antd";
|
||||
import Logo, { LogoType } from "../../../components/Logo";
|
||||
import { useRecoilState, useSetRecoilState } from "recoil";
|
||||
|
||||
// import { GlobalHotKeys } from "react-hotkeys";
|
||||
// import { STORE_ITEMS } from "../../../electron/constants";
|
||||
// import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
// import UserAvatarWithStatus from "../../../components/User/userAvatarWithStatus";
|
||||
// import { UserStatus } from "@nirvana/core/models";
|
||||
// import { socket } from "../../../nirvanaApp";
|
||||
// // import { useGetUserDetails } from "../../../controller/index";
|
||||
// import { useRef } from "react";
|
||||
import { GlobalHotKeys } from "react-hotkeys";
|
||||
import { STORE_ITEMS } from "../../../electron/constants";
|
||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
import UserAvatarWithStatus from "../../../components/User/userAvatarWithStatus";
|
||||
import { UserStatus } from "@nirvana/core/models";
|
||||
import { socket } from "../../../nirvanaApp";
|
||||
import { useGetUserDetails } from "../../../controller/index";
|
||||
import { useRef } from "react";
|
||||
|
||||
// export default function Header() {
|
||||
// // const { data: user, isLoading } = useGetUserDetails();
|
||||
// const [searchQuery, setSearchQuery] = useRecoilState($searchQuery);
|
||||
export default function Header() {
|
||||
const { data: userDetailsResponse, isLoading } = useGetUserDetails();
|
||||
const [searchQuery, setSearchQuery] = useRecoilState($searchQuery);
|
||||
|
||||
// const inputRef = useRef(null);
|
||||
const inputRef = useRef(null);
|
||||
|
||||
// const setAuthTokens = useSetRecoilState($authTokens);
|
||||
const setAuthTokens = useSetRecoilState($authTokens);
|
||||
|
||||
// if (isLoading) {
|
||||
// return <span>getting data</span>;
|
||||
// }
|
||||
if (isLoading) {
|
||||
return <span>getting data</span>;
|
||||
}
|
||||
|
||||
// const logOut = () => {
|
||||
// window.electronAPI.store.set(STORE_ITEMS.GOOGLE_AUTH_TOKENS, null);
|
||||
// setAuthTokens(null);
|
||||
// };
|
||||
const logOut = () => {
|
||||
window.electronAPI.store.set(STORE_ITEMS.GOOGLE_AUTH_TOKENS, null);
|
||||
setAuthTokens(null);
|
||||
};
|
||||
|
||||
// const updateStatus = (newStatus: UserStatus) => {
|
||||
// // send update in socket
|
||||
// socket.emit(
|
||||
// SocketChannels.SEND_USER_STATUS_UPDATE,
|
||||
// user.googleId,
|
||||
// newStatus
|
||||
// );
|
||||
// };
|
||||
const updateStatus = (newStatus: UserStatus) => {
|
||||
// send update in socket
|
||||
socket.emit(
|
||||
SocketChannels.SEND_USER_STATUS_UPDATE,
|
||||
userDetailsResponse.user.status,
|
||||
newStatus
|
||||
);
|
||||
};
|
||||
|
||||
// const ProfileMenu = (
|
||||
// <Menu>
|
||||
// <Menu.Item key="0">
|
||||
// <button onClick={logOut}>Log Out</button>
|
||||
// </Menu.Item>
|
||||
// <Menu.Item key="1">
|
||||
// {user.status === UserStatus.ONLINE ? (
|
||||
// <button onClick={() => updateStatus(UserStatus.OFFLINE)}>
|
||||
// Set status as away
|
||||
// </button>
|
||||
// ) : (
|
||||
// <button onClick={() => updateStatus(UserStatus.ONLINE)}>
|
||||
// Set status as online
|
||||
// </button>
|
||||
// )}
|
||||
// </Menu.Item>
|
||||
// </Menu>
|
||||
// );
|
||||
const ProfileMenu = (
|
||||
<Menu>
|
||||
<Menu.Item key="0">
|
||||
<button onClick={logOut}>Log Out</button>
|
||||
</Menu.Item>
|
||||
<Menu.Item key="1">
|
||||
{userDetailsResponse.user.status === UserStatus.ONLINE ? (
|
||||
<button onClick={() => updateStatus(UserStatus.OFFLINE)}>
|
||||
Set status as away
|
||||
</button>
|
||||
) : (
|
||||
<button onClick={() => updateStatus(UserStatus.ONLINE)}>
|
||||
Set status as online
|
||||
</button>
|
||||
)}
|
||||
</Menu.Item>
|
||||
</Menu>
|
||||
);
|
||||
|
||||
// // hot keys for selecting search
|
||||
// const handleSearch = () => {
|
||||
// if (inputRef?.current) {
|
||||
// inputRef?.current?.focus();
|
||||
// setSearchQuery("");
|
||||
// }
|
||||
// };
|
||||
// hot keys for selecting search
|
||||
const handleSearch = () => {
|
||||
if (inputRef?.current) {
|
||||
inputRef?.current?.focus();
|
||||
setSearchQuery("");
|
||||
}
|
||||
};
|
||||
|
||||
// const keyMap = { START_SEARCH: "/" };
|
||||
// const handlers = { START_SEARCH: handleSearch };
|
||||
const keyMap = { START_SEARCH: "/" };
|
||||
const handlers = { START_SEARCH: handleSearch };
|
||||
|
||||
// return (
|
||||
// <>
|
||||
// <GlobalHotKeys handlers={handlers} keyMap={keyMap}></GlobalHotKeys>
|
||||
return (
|
||||
<>
|
||||
<GlobalHotKeys handlers={handlers} keyMap={keyMap}></GlobalHotKeys>
|
||||
|
||||
// <div className="flex flex-row items-center bg-zinc-800 h-20 px-5">
|
||||
// <Logo type={LogoType.small} className="scale-[0.4]" />
|
||||
// <input
|
||||
// ref={inputRef}
|
||||
// placeholder="type / to search"
|
||||
// className="placeholder:text-zinc-400 bg-transparent outline-none text-zinc-100"
|
||||
// value={searchQuery}
|
||||
// onChange={(e) => setSearchQuery(e.target.value)}
|
||||
// />
|
||||
<div className="flex flex-row items-center bg-zinc-800 h-20 px-5">
|
||||
<Logo type={LogoType.small} className="scale-[0.4]" />
|
||||
<input
|
||||
ref={inputRef}
|
||||
placeholder="type / to search"
|
||||
className="placeholder:text-zinc-400 bg-transparent outline-none text-zinc-100"
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
/>
|
||||
|
||||
// <button
|
||||
// onClick={() => updateStatus(UserStatus.FLOW_STATE)}
|
||||
// className="ml-auto mr-2 hover:scale-110 bg-zinc-600 text-teal-500 py-1 px-2 rounded-lg text-sm"
|
||||
// >
|
||||
// flow state
|
||||
// </button>
|
||||
<button
|
||||
onClick={() => updateStatus(UserStatus.FLOW_STATE)}
|
||||
className="ml-auto mr-2 hover:scale-110 bg-zinc-600 text-teal-500 py-1 px-2 rounded-lg text-sm"
|
||||
>
|
||||
flow state
|
||||
</button>
|
||||
|
||||
// <Dropdown overlay={ProfileMenu} trigger={["click"]}>
|
||||
// <span className="px-5">
|
||||
// <UserAvatarWithStatus user={user} />
|
||||
// </span>
|
||||
// </Dropdown>
|
||||
// </div>
|
||||
// </>
|
||||
// );
|
||||
// }
|
||||
<Dropdown overlay={ProfileMenu} trigger={["click"]}>
|
||||
<span className="px-5">
|
||||
{userDetailsResponse?.user && (
|
||||
<UserAvatarWithStatus user={userDetailsResponse?.user} />
|
||||
)}
|
||||
</span>
|
||||
</Dropdown>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import Logo, { LogoType } from "../../components/Logo";
|
||||
|
||||
import { $searchQuery } from "../../controller/recoil";
|
||||
import Conversations from "./conversations";
|
||||
import Header from "./header";
|
||||
// import Header from "./header";
|
||||
import Search from "./search";
|
||||
// import SelectedConversation from "./selectedConversation";
|
||||
|
||||
@@ -42,7 +42,9 @@ export default function Login() {
|
||||
|
||||
window.electronAPI.store.set(STORE_ITEMS.AUTH_SESSION_JWT, jwtToken);
|
||||
|
||||
window.location.reload();
|
||||
console.log(loginResponse);
|
||||
|
||||
// window.location.reload();
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user