adding bunch of stuff for user getting
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
export default interface IFirestoreSerializable {
|
||||
id: string;
|
||||
|
||||
serialize: () => {};
|
||||
|
||||
// deserialize: (firestoreData: {}) => {};
|
||||
}
|
||||
+20
-2
@@ -1,12 +1,30 @@
|
||||
import { documentId, Firestore, serverTimestamp, Timestamp } from "firebase/firestore";
|
||||
import IFirestoreSerializable from "./firestoreSerializable";
|
||||
|
||||
export class User {
|
||||
id: string;
|
||||
nickname: string;
|
||||
nickName: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
|
||||
createdDate: Date
|
||||
lastUpdatedDate: Date
|
||||
|
||||
// serialize() {
|
||||
// return {
|
||||
// createdDate: Timestamp.fromDate(this.createdDate),
|
||||
// lastUpdatedDate: Timestamp.fromDate(this.lastUpdatedDate)
|
||||
// }
|
||||
// }
|
||||
|
||||
// deserialize(firestoreData: {}) {
|
||||
// this.lastUpdatedDate = firestoreData.lastUpdatedDate.toDate()
|
||||
// }
|
||||
}
|
||||
|
||||
export enum UserStatus {
|
||||
online = "online",
|
||||
offline = "offline",
|
||||
inCall = "in call",
|
||||
busy = "busy"
|
||||
busy = "busy"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"type": "service_account",
|
||||
"project_id": "nirvana-for-business",
|
||||
"private_key_id": "8720d0ab82c42545bb151e3e394d03d81acdb208",
|
||||
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC9aX62Q555qu4D\nUCmyl3NZda5XDJt5Y+ogpE+ModB8poGiiywX2P/zzR6FhmuCVSKseI4ehHsZn2T8\nBAokBJy6ovsg5UhSQ0Q6vbeJ7mVvyEbyofAElz651CTHk2Tx5iQZbE27vgFRD9zn\nuMyNuCaVpc4D1It1zDxwNNk0y5Na5Pitke21h4NznAh5KTfmbc6L0Sf+E1NvhybO\n+DHSgBBTHOiXAgxWMlCH6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M+ARLxt7jWiQQj6N7tTNo2pOgPi1j\nxTztD0MvduHReFn+Yh9MoVi6B5sflJvz3RvgMEd7E3iMPhfOoYbr3TbhuXLI/Zfk\n0Zvr5e10zRemHwP92eXM23hA1NcVt/r11+m/EAECgYEA+AAxyEaaTv63j2PImwvO\nKsqtzpHsf97PbsVnj6A9UAGXm80vU3678X8OdJAw0zFlub7LqcL7ses9MXHgxJmb\n7WYWWJSt6phDXgrQeBx9imLZpPywQKCg2Xsstum9veCyfGB6UHfvDWPK8vIF15lz\nV0IUN4EnE6YsG9dbeYEGX+0=\n-----END PRIVATE KEY-----\n",
|
||||
"client_email": "firebase-adminsdk-e05xh@nirvana-for-business.iam.gserviceaccount.com",
|
||||
"client_id": "109842603410185922994",
|
||||
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
||||
"token_uri": "https://oauth2.googleapis.com/token",
|
||||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
||||
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-e05xh%40nirvana-for-business.iam.gserviceaccount.com"
|
||||
}
|
||||
Generated
+2030
-12
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@
|
||||
"dependencies": {
|
||||
"antd": "^4.18.3",
|
||||
"firebase": "^9.6.2",
|
||||
"firebase-admin": "^10.0.1",
|
||||
"next": "12.0.7",
|
||||
"react": "17.0.2",
|
||||
"react-dom": "17.0.2",
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
|
||||
import { credential } from 'firebase-admin';
|
||||
import { initializeApp } from 'firebase-admin/app';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
import serviceAccount from '../../../nirvana-for-business-firebase-adminsdk.json'
|
||||
|
||||
initializeApp({
|
||||
credential: credential.cert({
|
||||
privateKey: serviceAccount.private_key,
|
||||
clientEmail: serviceAccount.client_email,
|
||||
projectId: serviceAccount.project_id,
|
||||
})
|
||||
});
|
||||
|
||||
export default validateToken = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
console.log('Validating token...');
|
||||
try {
|
||||
const { token } = JSON.parse(req.headers.authorization || '{}');
|
||||
if (!token) {
|
||||
return res.status(403).send({
|
||||
errorCode: 403,
|
||||
message: 'Auth token missing.'
|
||||
});
|
||||
}
|
||||
const result = await validate(token);
|
||||
return res.status(200).send(result);
|
||||
} catch (err) {
|
||||
return res.status(err.code).send({
|
||||
errorCode: err.code,
|
||||
message: err.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { useAuth } from "../../contexts/authContext"
|
||||
import { useRouter } from "next/router"
|
||||
import { useEffect } from "react"
|
||||
|
||||
/**
|
||||
* figure out where to take the user based on everything
|
||||
*/
|
||||
function RouteHandler() {
|
||||
const { currUser, logOut } = useAuth()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
// if not authenticated, take user to the login
|
||||
if (!currUser) {
|
||||
console.log('not authenticated...routing from dashboard to teams home')
|
||||
router.push('/teams/login')
|
||||
}
|
||||
}, [currUser])
|
||||
|
||||
return (
|
||||
<div className="text-white">this is the route handler page</div>
|
||||
)
|
||||
}
|
||||
|
||||
export async function getServerSideProps() {
|
||||
// Pass data to the page via props
|
||||
return { props: { } }
|
||||
}
|
||||
|
||||
export default RouteHandler
|
||||
@@ -0,0 +1,51 @@
|
||||
import { useAuth } from "../../contexts/authContext"
|
||||
import { useRouter } from "next/router"
|
||||
import { useEffect } from "react"
|
||||
import UserService from '../../services/userService'
|
||||
import { User } from "../../models/user"
|
||||
import { GetServerSidePropsContext } from "next"
|
||||
|
||||
/**
|
||||
* figure out where to take the user based on everything
|
||||
*/
|
||||
function RouteHandler({ user }) {
|
||||
const { currUser, logOut } = useAuth()
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
// if not authenticated, take user to the login
|
||||
if (!currUser) {
|
||||
console.log('not authenticated...routing from dashboard to teams home')
|
||||
router.push('/teams/login')
|
||||
}
|
||||
|
||||
// if user has no profile, then go to create profile
|
||||
|
||||
}, [currUser])
|
||||
|
||||
return (
|
||||
<div className="text-black">this is the route handler page</div>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
// get user data
|
||||
const userService: UserService = new UserService()
|
||||
|
||||
var user: User | null = null
|
||||
|
||||
try {
|
||||
user = await userService.getUser("asdf")
|
||||
} catch(error) {
|
||||
console.log(error)
|
||||
}
|
||||
|
||||
// Pass data to the page via props
|
||||
return { props: {
|
||||
user
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default RouteHandler
|
||||
@@ -11,8 +11,11 @@
|
||||
|
||||
|
||||
// Import the functions you need from the SDKs you need
|
||||
import { initializeApp } from "firebase/app";
|
||||
import * as firebase from 'firebase/app';
|
||||
import { getAnalytics } from "firebase/analytics";
|
||||
import { getFirestore } from "firebase/firestore";
|
||||
import { browserSessionPersistence, getAuth, setPersistence } from 'firebase/auth';
|
||||
|
||||
// TODO: Add SDKs for Firebase products that you want to use
|
||||
// https://firebase.google.com/docs/web/setup#available-libraries
|
||||
|
||||
@@ -29,5 +32,10 @@ const firebaseConfig = {
|
||||
};
|
||||
|
||||
// Initialize Firebase
|
||||
const app = initializeApp(firebaseConfig);
|
||||
const analytics = getAnalytics(app);
|
||||
const app = firebase.initializeApp(firebaseConfig);
|
||||
const analytics = getAnalytics(app);
|
||||
|
||||
console.log('initialized firebase')
|
||||
|
||||
setPersistence(getAuth(), browserSessionPersistence)
|
||||
|
||||
|
||||
@@ -1,5 +1,37 @@
|
||||
class UserService {
|
||||
|
||||
}
|
||||
import { User as FirUser } from "firebase/auth";
|
||||
import { Firestore, getFirestore, doc, getDoc } from "firebase/firestore";
|
||||
import { User } from '../models/user'
|
||||
|
||||
export {}
|
||||
export default class UserService {
|
||||
private db : Firestore = getFirestore()
|
||||
private static collectionName: string = "users"
|
||||
|
||||
constructor() {
|
||||
|
||||
}
|
||||
|
||||
// give back the avatar based on the person's google account avatar or if they set one then
|
||||
getUserAvatar(firstName: string, lastName: string, avatar: string = null) {
|
||||
if (avatar) {
|
||||
return avatar
|
||||
}
|
||||
|
||||
return `https://ui-avatars.com/api/?name=${firstName}+${lastName}`
|
||||
}
|
||||
|
||||
async getUser(userId: string) : Promise<User | null> {
|
||||
const docRef = doc(this.db, UserService.collectionName, userId);
|
||||
const docSnap = await getDoc(docRef);
|
||||
|
||||
if (docSnap.exists()) {
|
||||
console.log("Document data:", docSnap.data());
|
||||
let user: User = docSnap.data() as User
|
||||
return user
|
||||
} else {
|
||||
// doc.data() will be undefined in this case
|
||||
console.log("user not found!");
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user