created new Schema for userInfo & update admin panel
This commit is contained in:
parent
bd68d3f97e
commit
cc709d5bd5
6 changed files with 85 additions and 33 deletions
|
@ -16,30 +16,7 @@ const UserSchema = new Schema({
|
|||
image:{
|
||||
type: String,
|
||||
},
|
||||
phoneNumber:{
|
||||
type: String,
|
||||
},
|
||||
streetAddress:{
|
||||
type: String,
|
||||
},
|
||||
city:{
|
||||
type: String,
|
||||
},
|
||||
stateProvince:{
|
||||
type: String,
|
||||
},
|
||||
zipCode:{
|
||||
type: String,
|
||||
},
|
||||
country:{
|
||||
type: String,
|
||||
},
|
||||
admin:{
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
|
||||
|
||||
|
||||
}, {timestamps: true});
|
||||
|
||||
export const User = models?.User || model('User', UserSchema);
|
33
donutshop_ecommerce/src/app/api/models/UserInfo.js
Normal file
33
donutshop_ecommerce/src/app/api/models/UserInfo.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { Schema, model, models } from "mongoose";
|
||||
|
||||
|
||||
const UserInfoSchema = new Schema({
|
||||
email:{
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
phoneNumber:{
|
||||
type: String,
|
||||
},
|
||||
streetAddress:{
|
||||
type: String,
|
||||
},
|
||||
city:{
|
||||
type: String,
|
||||
},
|
||||
stateProvince:{
|
||||
type: String,
|
||||
},
|
||||
zipCode:{
|
||||
type: String,
|
||||
},
|
||||
country:{
|
||||
type: String,
|
||||
},
|
||||
admin:{
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
}, {timestamps: true})
|
||||
|
||||
export const UserInfo = models?.UserInfo || model('UserInfo', UserInfoSchema)
|
|
@ -2,15 +2,20 @@ import mongoose from "mongoose"
|
|||
import { getServerSession } from "next-auth";
|
||||
import {authOptions} from "../auth/[...nextauth]/route"
|
||||
import { User } from "../models/User";
|
||||
import { UserInfo } from "../models/UserInfo"
|
||||
|
||||
export async function PUT(req){
|
||||
mongoose.connect(process.env.MONGO_URL)
|
||||
const data = await req.json();
|
||||
const session = await getServerSession(authOptions)
|
||||
const email = session.user.email
|
||||
|
||||
|
||||
await User.updateOne({email}, data);
|
||||
const {name, image, ...otherUserInfo} = data
|
||||
|
||||
await User.updateOne({email}, {name, image});
|
||||
|
||||
//update userInfo
|
||||
await UserInfo.findOneAndUpdate({email}, otherUserInfo, {upsert:true});
|
||||
|
||||
|
||||
return Response.json(true)
|
||||
}
|
||||
|
@ -18,9 +23,11 @@ export async function PUT(req){
|
|||
export async function GET(){
|
||||
mongoose.connect(process.env.MONGO_URL)
|
||||
const session = await getServerSession(authOptions)
|
||||
const email = session.user.email
|
||||
|
||||
return Response.json(
|
||||
await User.findOne({email})
|
||||
)
|
||||
const email = session?.user?.email
|
||||
if(!email){
|
||||
return Response.json({})
|
||||
}
|
||||
const user = await User.findOne({email}).lean()
|
||||
const userInfo = await UserInfo.findOne({email}).lean()
|
||||
return Response.json({...user, ...userInfo})
|
||||
}
|
|
@ -1,7 +1,20 @@
|
|||
"use client"
|
||||
import React from 'react'
|
||||
import UserTab from '../../components/layout/UserTab'
|
||||
import useProfile from '../../components/UseProfile'
|
||||
|
||||
const CategoriesPage = () => {
|
||||
const {loading:profileLoading, data:profileData} = useProfile();
|
||||
|
||||
if(profileLoading){
|
||||
return <p className='flex justify-center items-center'>Loading...</p>
|
||||
}
|
||||
|
||||
if(!profileData.admin){
|
||||
return <p className='flex justify-center items-center'>Please login as an admin</p>
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className='px-5 mb-10 max-w-lg mx-auto'>
|
||||
<UserTab isAdmin={true}/>
|
||||
|
|
|
@ -104,7 +104,7 @@ const ProfilePage = () => {
|
|||
return (
|
||||
<div className='px-5 mb-10'>
|
||||
<UserTabs isAdmin={isAdmin}/>
|
||||
<div className='max-w-md mx-auto mt-5'>
|
||||
<div className='max-w-md mx-auto mt-10'>
|
||||
<div className='flex flex-col gap-2 justify-center items-center max-w-[100px] mx-auto'>
|
||||
{image && (
|
||||
<Image src={image} width={100} height={100} alt={'user-image'} className='rounded-full'/>
|
||||
|
|
22
donutshop_ecommerce/src/components/UseProfile.js
Normal file
22
donutshop_ecommerce/src/components/UseProfile.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
import { useEffect, useState } from 'react'
|
||||
|
||||
const useProfile = () => {
|
||||
const [data, setData] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
//setup admin page
|
||||
useEffect(() => {
|
||||
setLoading(true)
|
||||
fetch('/api/profile').then(response => {
|
||||
response.json().then(data => {
|
||||
setData(data);
|
||||
setLoading(false)
|
||||
});
|
||||
})
|
||||
}, []);
|
||||
|
||||
|
||||
return {loading, data}
|
||||
}
|
||||
|
||||
export default useProfile
|
Loading…
Add table
Reference in a new issue