cleaning a bunch of the auth stuff but not done
This commit is contained in:
@@ -1,175 +1,40 @@
|
||||
import { $authTokens, $searchQuery } from "./recoil";
|
||||
import axios, { AxiosResponse } from "axios";
|
||||
import { queryClient, socket } from "../nirvanaApp";
|
||||
import { useEffect, useState } from "react";
|
||||
import NirvanaApi, { login } from "./nirvanaApi";
|
||||
import { useMutation, useQuery } from "react-query";
|
||||
|
||||
import GetContactsResponse from "../../../core/responses/getContacts.response";
|
||||
import GetConversationDetailsResponse from "@nirvana/core/responses/getConversationDetails.response";
|
||||
import { ObjectId } from "mongodb";
|
||||
import { RelationshipState } from "@nirvana/core/models/relationship.model";
|
||||
import SearchResponse from "@nirvana/core/responses/search.response";
|
||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
import UpdateRelationshipStateRequest from "../../../core/requests/updateRelationshipState.request";
|
||||
import { User } from "@nirvana/core/models";
|
||||
import { nirvanaApi } from "./nirvanaApi";
|
||||
import { $authTokens } from "./recoil";
|
||||
import { useRecoilValue } from "recoil";
|
||||
|
||||
// =========== API
|
||||
export const localHost = "http://localhost:5000/api";
|
||||
|
||||
const getUserDetails = async (accessToken: string, idToken: string) => {
|
||||
const response = await axios.get<User>(
|
||||
localHost + `/users?access_token=${accessToken}`,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const search = async (idToken: string, searchQuery: string) => {
|
||||
const response = await axios.get<SearchResponse>(
|
||||
localHost + `/search?query=${searchQuery}`,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getConversationDetails = async (
|
||||
idToken: string,
|
||||
otherUserGoogleId: string
|
||||
) => {
|
||||
const response = await axios.get<GetConversationDetailsResponse>(
|
||||
localHost + `/conversations/${otherUserGoogleId}`,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const sendContactRequest = async (
|
||||
idToken: string,
|
||||
otherUserGoogleId: string
|
||||
) => {
|
||||
const response = await axios.post(
|
||||
localHost + `/contacts/${otherUserGoogleId}`,
|
||||
null,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const updateContactRequestState = async (
|
||||
idToken: string,
|
||||
reqObj: UpdateRelationshipStateRequest
|
||||
) => {
|
||||
const response = await axios.put(localHost + `/contacts`, reqObj, {
|
||||
headers: { Authorization: idToken },
|
||||
});
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
const getContactsBasicDetails = async (idToken: string) => {
|
||||
const response = await axios.get<GetContactsResponse>(
|
||||
localHost + `/contacts`,
|
||||
{
|
||||
headers: { Authorization: idToken },
|
||||
}
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// ====== QUERIES
|
||||
export enum Querytypes {
|
||||
GET_USER_DETAILS = "GET_USER_DETAILS",
|
||||
GET_SEARCH_RESULTS = "GET_SEARCH_RESULTS",
|
||||
GET_CONVERSATION_DETAILS = "GET_CONVERSATION_DETAILS",
|
||||
GET_CONTACTS_RELATIONSHIPS = "GET_CONTACTS_RELATIONSHIPS",
|
||||
|
||||
export function useLogin() {
|
||||
return useMutation("LOGIN", login, {});
|
||||
}
|
||||
|
||||
export function useGetUserDetails() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
// 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);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
// return useQuery(
|
||||
// Querytypes.GET_USER_DETAILS,
|
||||
// () => getUserDetails(authTokens?.accessToken, authTokens?.idToken),
|
||||
// {
|
||||
// retry: false,
|
||||
// refetchOnWindowFocus: false,
|
||||
// onError: (err) => {
|
||||
// console.log(err);
|
||||
// },
|
||||
// }
|
||||
// );
|
||||
// }
|
||||
|
||||
export function useSearch() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
const searchQuery = useRecoilValue($searchQuery);
|
||||
// export function useSearch() {
|
||||
// const authTokens = useRecoilValue($authTokens);
|
||||
// const searchQuery = useRecoilValue($searchQuery);
|
||||
|
||||
return useQuery(
|
||||
Querytypes.GET_SEARCH_RESULTS,
|
||||
() => search(authTokens.idToken, searchQuery),
|
||||
{ enabled: searchQuery ? true : false, refetchOnWindowFocus: false }
|
||||
);
|
||||
}
|
||||
|
||||
export function useConversationDetails(otherUserGoogleId: string) {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useQuery(
|
||||
Querytypes.GET_CONVERSATION_DETAILS + "/" + otherUserGoogleId,
|
||||
() => getConversationDetails(authTokens.idToken, otherUserGoogleId),
|
||||
{ enabled: otherUserGoogleId ? true : false }
|
||||
);
|
||||
}
|
||||
|
||||
export function useGetAllContactBasicDetails() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useQuery(
|
||||
Querytypes.GET_CONTACTS_RELATIONSHIPS,
|
||||
() => getContactsBasicDetails(authTokens.idToken),
|
||||
{
|
||||
refetchOnWindowFocus: false,
|
||||
}
|
||||
);
|
||||
}
|
||||
// return useQuery(
|
||||
// Querytypes.GET_SEARCH_RESULTS,
|
||||
// () => search(authTokens.idToken, searchQuery),
|
||||
// { enabled: searchQuery ? true : false, refetchOnWindowFocus: false }
|
||||
// );
|
||||
// }
|
||||
|
||||
// =========== MUTATIONS
|
||||
|
||||
export function useSendContactRequest() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useMutation((otherGoogleUserId: string) =>
|
||||
sendContactRequest(authTokens.idToken, otherGoogleUserId)
|
||||
);
|
||||
}
|
||||
|
||||
export function useUpdateRelationshipState() {
|
||||
const authTokens = useRecoilValue($authTokens);
|
||||
|
||||
return useMutation(
|
||||
(updateReqObj: UpdateRelationshipStateRequest) =>
|
||||
updateContactRequestState(authTokens.idToken, updateReqObj),
|
||||
{
|
||||
onSettled: (data, error) => {
|
||||
return queryClient.invalidateQueries(
|
||||
Querytypes.GET_CONVERSATION_DETAILS + "/" + ""
|
||||
);
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
||||
import axios, { AxiosRequestConfig, AxiosResponse, Method } from "axios";
|
||||
|
||||
import LoginResponse from "../../../core/responses/login.response";
|
||||
import { User } from "@nirvana/core/models";
|
||||
|
||||
// export const localHost = process.env.REACT_APP_API_DOMAIN;
|
||||
|
||||
export const localHost = "http://localhost:5000/api";
|
||||
|
||||
class NirvanaApi {
|
||||
export default class NirvanaApi {
|
||||
// auth token from google that our backend will use
|
||||
private _authToken?: string;
|
||||
static _jwtToken?: string;
|
||||
|
||||
setGoogleIdToken(_googleIdToken: string) {
|
||||
this._authToken = _googleIdToken;
|
||||
}
|
||||
|
||||
async fetch(url: string, method: string, privateRoute = false) {
|
||||
static async fetch(url: string, method: Method, privateRoute = false) {
|
||||
// use the auth token if it's a private route
|
||||
// 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
|
||||
@@ -22,16 +19,22 @@ class NirvanaApi {
|
||||
try {
|
||||
const fullUrl = localHost + url;
|
||||
|
||||
var res;
|
||||
if (privateRoute && this._authToken) {
|
||||
let res;
|
||||
if (privateRoute && this._jwtToken) {
|
||||
res = await fetch(fullUrl, {
|
||||
method: method,
|
||||
headers: { Authorization: this._authToken },
|
||||
headers: { Authorization: this._jwtToken },
|
||||
});
|
||||
} else {
|
||||
res = await fetch(fullUrl);
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
if (res.status === 401) throw Error("You are not authorized here");
|
||||
|
||||
throw Error("Something went wrong");
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@@ -39,19 +42,19 @@ class NirvanaApi {
|
||||
throw Error(error);
|
||||
}
|
||||
}
|
||||
|
||||
user = {
|
||||
async getUserDetails(accessToken: string): Promise<User> {
|
||||
return await axios.get(localHost + `/users?access_token=${accessToken}`, {
|
||||
headers: { Authorization: this._authToken },
|
||||
});
|
||||
},
|
||||
async createUser(accessToken: string) {
|
||||
return await axios.post(
|
||||
localHost + `/users/create?access_token=${accessToken}`
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const nirvanaApi = new NirvanaApi();
|
||||
export async function login(reqLoginTokens: {
|
||||
accessToken: string;
|
||||
idToken: string;
|
||||
}): Promise<LoginResponse> {
|
||||
return await NirvanaApi.fetch(
|
||||
`/user/login?access_token=${reqLoginTokens.accessToken}&id_token=${reqLoginTokens.idToken}`,
|
||||
"GET",
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
export async function authCheck(jwtToken: string) {
|
||||
return await NirvanaApi.fetch(`/user/authCheck`, "GET", true);
|
||||
}
|
||||
|
||||
@@ -14,12 +14,7 @@ export const $searchQuery = atom<string>({
|
||||
default: "",
|
||||
});
|
||||
|
||||
export const $authFailureCount = atom<number>({
|
||||
key: "AUTH_FAILURE_COUNT",
|
||||
default: 0,
|
||||
});
|
||||
|
||||
// google Id of the selected person/conversation
|
||||
// conversation id
|
||||
export const $selectedConversation = atom<string>({
|
||||
key: "SELECTED_CONVERSATION",
|
||||
default: null,
|
||||
|
||||
@@ -3,26 +3,27 @@ import { useEffect, useState } from "react";
|
||||
import SocketChannels from "@nirvana/core/sockets/channels";
|
||||
import { UserStatus } from "../../../core/models/user.model";
|
||||
import { socket } from "../nirvanaApp";
|
||||
import { useGetAllContactBasicDetails } from "./index";
|
||||
|
||||
// import { useGetAllContactBasicDetails } from "./index";
|
||||
|
||||
export default function useSocketData() {
|
||||
// relationshipId's of the conversations where there is someone speaking
|
||||
const [speakingRooms, setSpeakingRooms] = useState<string[]>([]);
|
||||
|
||||
const { data: allConvosDetsResponse, isFetched } =
|
||||
useGetAllContactBasicDetails();
|
||||
// const { data: allConvosDetsResponse, isFetched } =
|
||||
// useGetAllContactBasicDetails();
|
||||
|
||||
useEffect(() => {
|
||||
if (allConvosDetsResponse) {
|
||||
allConvosDetsResponse.contactsDetails.map((contactDet) => {
|
||||
// join the right rooms based on the relevant contacts/conversations returned here
|
||||
socket.emit(
|
||||
SocketChannels.JOIN_ROOM,
|
||||
contactDet.relationship._id.toString()
|
||||
);
|
||||
});
|
||||
}
|
||||
}, [isFetched]);
|
||||
// useEffect(() => {
|
||||
// if (allConvosDetsResponse) {
|
||||
// allConvosDetsResponse.contactsDetails.map((contactDet) => {
|
||||
// // join the right rooms based on the relevant contacts/conversations returned here
|
||||
// socket.emit(
|
||||
// SocketChannels.JOIN_ROOM,
|
||||
// contactDet.relationship._id.toString()
|
||||
// );
|
||||
// });
|
||||
// }
|
||||
// }, [isFetched]);
|
||||
|
||||
useEffect(() => {
|
||||
socket.on(
|
||||
|
||||
Reference in New Issue
Block a user