Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion containers/Dashboard/components/FollowerList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Loading from '../../../components/Loading';
import { IModalType } from '..';
import * as Service from '../../../services';
import { useNotification } from '../../../context/notification';
import { User } from '../../../prisma/user';
import FollowerUnit from './FollowerUnit';
import { User } from '.prisma/client';

type IFollowerListProps = {
modalType: IModalType;
Expand Down
2 changes: 1 addition & 1 deletion containers/Dashboard/components/FollowerUnit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Image from 'next/image';
import { User } from '@prisma/client';
import TextButton from '../../../components/UI/TextButton';
import { User } from '../../../prisma/user';

type IFollowerUnitProps = {
follower: User;
Expand Down
2 changes: 1 addition & 1 deletion containers/Dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { useNotification } from '../../context/notification';
import * as SecureServices from '../../services/secure';
import FollowButton from '../../components/UI/FollowButton';
import { useAuth } from '../../context/auth';
import { User } from '../../prisma/user';
import FollowerList from './components/FollowerList';
import { DashboardModalContainer } from './styles';
import { User } from '.prisma/client';

type IDashboardProps = {
profileUser: User;
Expand Down
39 changes: 17 additions & 22 deletions containers/Header/forms/AccountSetup.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React, { useState } from 'react';
import { Link, Typography } from '@mui/material';
import axios, { AxiosResponse, AxiosError } from 'axios';
import FormInput from '../../../components/UI/FormInput';
import FormButton from '../../../components/UI/FormButton';
import { useNotification } from '../../../context/notification';
import { ICreateUsernameResponse } from '../../../pages/api/user/username/[email]';
import { useAuth } from '../../../context/auth';
import { profanityFilter, validateUsername } from '../../../utils/Filter';
import * as SecureServices from '../../../services/secure';
import { FormContainer, FormContent } from './styles';

type ISignupProps = {
Expand All @@ -16,7 +15,6 @@ type ISignupProps = {

const AccountSetup = (props: ISignupProps) => {
const { close, logout } = props;
const { user } = useAuth();
const { setUsername: _setUsername } = useAuth();
const { setNotification } = useNotification();
const [username, setUsername] = useState<string>('');
Expand Down Expand Up @@ -48,25 +46,22 @@ const AccountSetup = (props: ISignupProps) => {
timeout: 7000,
});
}
axios
.post(`/api/user/username/${user.email}`, { username })
.then((res: AxiosResponse<ICreateUsernameResponse>) => {
if (res.data.status === 'success') {
_setUsername(res.data.username);
close();
}
})
.catch((err: AxiosError<ICreateUsernameResponse>) => {
const message = err.response.data.message;
if (message) {
setNotification({
message,
status: 'warning',
});
} else {
setNotification({ message: err.message, status: 'error' });
}
});
SecureServices.createUsername(username).then((res) => {
if (res.status === 'error') {
return setNotification({
message: res.message,
status: 'error',
});
}
if (res.status === 'rejected') {
return setNotification({
message: res.message,
status: 'warning',
});
}
_setUsername(res.username);
close();
});
};

const FORM_HEIGHT = 250;
Expand Down
2 changes: 1 addition & 1 deletion context/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useState, useContext } from 'react';
import { useDeepCompareEffect } from '../utils/hooks';
import * as SecureServices from '../services/secure';
import { User } from '../prisma/user';
import { IAuthContext, IAuthState, _void } from './types';
import { User } from '.prisma/client';

export const initialAuthState: IAuthState = {
user: undefined,
Expand Down
2 changes: 1 addition & 1 deletion context/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dispatch, SetStateAction } from 'react';
import { User } from '.prisma/client';
import { User } from '../prisma/user';

export type INotification = {
message: string;
Expand Down
30 changes: 24 additions & 6 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { PrismaClient } from '@prisma/client';
import { PrismaClient as PrismaClientUser } from '../prisma/user';
// import { PrismaClient as PrismaClientAwards } from '../prisma/awards';

// PrismaClient is attached to the `global` object in development to prevent
// exhausting your database connection limit.
//
// Learn more:
// https://pris.ly/d/help/next-js-best-practices

Expand All @@ -16,16 +16,34 @@ declare global {
}

//@ts-ignore
const prisma: PrismaClient = (() => {
const prisma_user: PrismaClientUser = (() => {
if (process.env.NODE_ENV === 'production') {
return new PrismaClient();
return new PrismaClientUser();
}
// helps not exhaust db connection limit in development
// https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices
if (!global.prisma) {
global.prisma = new PrismaClient();
global.prisma = new PrismaClientUser();
}
return global.prisma;
})();

export default prisma;
//@ts-ignore
// const prisma_awards: PrismaClientAwards = (() => {
// if (process.env.NODE_ENV === 'production') {
// return new PrismaClientAwards();
// }
// // helps not exhaust db connection limit in development
// // https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices
// if (!global.prisma) {
// global.prisma = new PrismaClientAwards();
// }
// return global.prisma;
// })();

const Prisma = {
User: prisma_user,
// Awards: prisma_awards,
};

export default Prisma;
6 changes: 5 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ module.exports = {
images: {
domains: ['oscarexpert.nyc3.cdn.digitaloceanspaces.com'],
},
webpack: (config) => {
webpack: (config, { isServer }) => {
if (isServer) {
config.externals.push('_http_common');
config.externals.push('encoding');
}
return config;
},
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@mui/material": "^5.0.2",
"@mui/styles": "^5.0.1",
"@next-auth/prisma-adapter": "^0.5.4",
"@prisma/client": "^2.28.0",
"@prisma/client": "^2.20.0",
"@types/styled-components": "^5.1.15",
"aws-sdk": "^2.855.0",
"axios": "^0.21.4",
Expand Down Expand Up @@ -48,7 +48,7 @@
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.3.0",
"prisma": "^2.28.0",
"prisma": "^2.20.0",
"typescript": "^4.2.4"
},
"eslintConfig": {
Expand Down
6 changes: 3 additions & 3 deletions pages/api/auth/[...nextauth].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextApiHandler } from 'next';
import NextAuth, { NextAuthOptions } from 'next-auth';
import Adapters from 'next-auth/adapters';
import Providers from 'next-auth/providers';
import prisma from '../../../lib/prisma';
import Prisma from '../../../lib/prisma';

// https://www.youtube.com/watch?v=o_wZIVmWteQ
// https://github.com/nextauthjs/next-auth/blob/main/src/providers/email.js
Expand All @@ -19,9 +19,9 @@ const options: NextAuthOptions = {
from: process.env.SMTP_FROM,
}),
],
adapter: Adapters.Prisma.Adapter({ prisma }),
adapter: Adapters.Prisma.Adapter({ prisma: Prisma.User }),
secret: process.env.SECRET,
database: process.env.DATABASE_URL,
database: process.env.USER_DATABASE_URL,
callbacks: {
// Modify this to make new fields available for the session callback
session: async (session, user) => {
Expand Down
9 changes: 3 additions & 6 deletions pages/api/user/follow/[targetUserId].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/client';
import prisma from '../../../../lib/prisma';
import Prisma from '../../../../lib/prisma';
import { IApiResponse } from '../../../../types';

export type IFollow = IApiResponse;
Expand All @@ -18,12 +18,9 @@ export default async (req: NextApiRequest, res: NextApiResponse<IFollow>) => {
});
}

console.log('TARGET USER', targetUserId);
console.log('SESSION USER', session.user.id);

try {
if (method === 'POST') {
await prisma.follower.create({
await Prisma.User.follower.create({
data: {
userId: targetUserId,
followerId: session.user.id,
Expand All @@ -34,7 +31,7 @@ export default async (req: NextApiRequest, res: NextApiResponse<IFollow>) => {
});
}
if (method === 'DELETE') {
await prisma.follower.delete({
await Prisma.User.follower.delete({
where: {
userId_followerId: {
userId: targetUserId,
Expand Down
4 changes: 2 additions & 2 deletions pages/api/user/following/[profileUserId].ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/client';
import prisma from '../../../../lib/prisma';
import Prisma from '../../../../lib/prisma';
import { IApiResponse } from '../../../../types';

export interface ICheckIfFollowing extends IApiResponse {
Expand All @@ -23,7 +23,7 @@ export default async (

try {
if (method === 'GET') {
const result = await prisma.follower.findFirst({
const result = await Prisma.User.follower.findFirst({
where: {
userId,
followerId: session.user.id,
Expand Down
4 changes: 2 additions & 2 deletions pages/api/user/image/save.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/client';
import prisma from '../../../../lib/prisma';
import Prisma from '../../../../lib/prisma';
import { IApiResponse } from '../../../../types';

export type ISaveProfileImage = IApiResponse;
Expand All @@ -22,7 +22,7 @@ export default async (

try {
if (method === 'POST') {
await prisma.user.update({
await Prisma.User.user.update({
where: {
id: session.user.id,
},
Expand Down
4 changes: 2 additions & 2 deletions pages/api/user/image/upload/[fileName].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sharp from 'sharp';
import { NextApiRequest, NextApiResponse } from 'next';
import { getSession } from 'next-auth/client';
import { IApiResponse } from '../../../../../types';
import prisma from '../../../../../lib/prisma';
import Prisma from '../../../../../lib/prisma';

// IMPORTANT for formiable to parse the body
// Note: this makes us unable to read the body
Expand Down Expand Up @@ -92,7 +92,7 @@ export default async (

console.log('EDGE URL', edgeUrl);

await prisma.user.update({
await Prisma.User.user.update({
where: {
username,
},
Expand Down
6 changes: 3 additions & 3 deletions pages/api/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { User } from '@prisma/client';
import { getSession } from 'next-auth/client';
import { IApiResponse } from '../../../types';
import prisma from '../../../lib/prisma';
import Prisma from '../../../lib/prisma';
import { User } from '../../../prisma/user';

export interface IGetUserResponse extends IApiResponse {
user?: User;
Expand All @@ -23,7 +23,7 @@ export default async (

try {
if (method === 'GET') {
const user = await prisma.user.findUnique({
const user = await Prisma.User.user.findUnique({
where: {
id: session.user.id,
},
Expand Down
23 changes: 15 additions & 8 deletions pages/api/user/username/[email].ts → pages/api/user/username.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { Prisma } from '@prisma/client';
import { IApiResponse } from '../../../../types';
import prisma from '../../../../lib/prisma';
import { Prisma as PrismaClient } from '@prisma/client';
import { getSession } from 'next-auth/client';
import { IApiResponse } from '../../../types';
import Prisma from '../../../lib/prisma';

export interface ICreateUsernameResponse extends IApiResponse {
username?: string;
Expand All @@ -11,15 +12,21 @@ export default async (
req: NextApiRequest,
res: NextApiResponse<ICreateUsernameResponse>
) => {
const { method, query, body } = req;
const email = query.email as string;
const { method, body } = req;
const session = await getSession({ req });
if (!session) {
return res.status(401).send({
status: 'error',
message: 'User is not authenticated.',
});
}
const username = body.username as string;

try {
if (method === 'POST') {
const result = await prisma.user.update({
const result = await Prisma.User.user.update({
where: {
email,
id: session.user.id,
},
data: {
username,
Expand All @@ -33,7 +40,7 @@ export default async (
throw new Error();
} catch (e) {
console.error(e);
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e instanceof PrismaClient.PrismaClientKnownRequestError) {
let message = e.message;
if (e.code === 'P2002') {
message = 'This username has already been taken.';
Expand Down
4 changes: 2 additions & 2 deletions pages/api/users/[profileUserId]/follower/count.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { IApiResponse } from '../../../../../types';
import prisma from '../../../../../lib/prisma';
import Prisma from '../../../../../lib/prisma';

export interface IFollowerCountResponse extends IApiResponse {
count?: number;
Expand All @@ -16,7 +16,7 @@ export default async (
try {
// count how many followers id has
if (method === 'GET') {
const count = await prisma.follower.count({
const count = await Prisma.User.follower.count({
where: {
userId,
},
Expand Down
Loading