Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
syedslegend786 committed Apr 26, 2023
0 parents commit 1460079
Show file tree
Hide file tree
Showing 44 changed files with 5,243 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


# This was inserted by `prisma init`:
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="mongodb+srv://admin:[email protected]/market-place?retryWrites=true&w=majority"


#NEXT_AUTH
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_SECRET=123l;k12j3lk12j3kl12j3;l12kj3;l12j3

#STRIPE
STRIPE_PUBLISH_KEY=pk_test_51MUo50EhwvoKb8N0k2Q2Adcuxd7ncoDj7Va1qsWmcxbXjuJ1U6xyqHj5xLRkeuedfNNAi08bUNR4n08W0mNvclxs00j1zLJXRN
STRIPE_SECRET_KEY=sk_test_51MUo50EhwvoKb8N0BQGlnofyht55sT6HNfjoZf2ZTcRt0K7PPoJNGMdmALVD8zIzaOzbT7pbVpw5SLk7bGXfkyWg00WDeAeJPe
STRIPE_CREATE_INSTRUCTOR_REDIRECT_URL="http://localhost:3000/instructor/create-callback"
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file.

[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.ts`.

The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.

This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
30 changes: 30 additions & 0 deletions layouts/UserPages/UserPages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { makeInstructorApi } from '@/services/api/instructor'
import { Button } from '@/ui'
import { handleFrontEndResponse } from '@/utils/apiresponses'
import React from 'react'


interface UserPagesProps {
children: React.ReactElement
}
const UserPagesLayout = ({ children }: UserPagesProps) => {
const handleBecomeInstructor = async () => {
try {
const data = await makeInstructorApi()
window.location.href = data.data.url
} catch (error: any) {
const err = handleFrontEndResponse(error)
alert(err)
}
}
return (
<div>
<div>
<Button onClick={handleBecomeInstructor} text='Become Instructor' />
</div>
{children}
</div>
)
}

export { UserPagesLayout }
1 change: 1 addition & 0 deletions layouts/UserPages/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './UserPages'
1 change: 1 addition & 0 deletions layouts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './UserPages'
64 changes: 64 additions & 0 deletions lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import NextAuth, { AuthOptions } from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials"
import { PrismaAdapter } from "@next-auth/prisma-adapter"
import { db } from "@/lib/db"
import { Session } from "next-auth"


export const authOptions: AuthOptions = {
adapter: PrismaAdapter(db),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
},
async authorize(credentials, req) {
const { email, password } = credentials as { email: string, password: string }
const user = await db.user.findFirst({
where: {
AND: [
{
email: email,
},
{
password: password
}
]
}
})
if (user) {
const toReturn = {
id: user.id, roles: user.roles, email: user.email, name: user.name, image: ""
} as Session["user"]
return toReturn;
}
throw Error("incorrect credentials")
return null
}
})
],
session: {
strategy: "jwt"
},
secret: process.env.NEXTAUTH_SECRET as string,
callbacks: {
jwt: ({ token, user }) => {
if (user) {
token.id = user.id
token.name = user.name
token.email = user.email
token.roles = user.roles
}
return token
},
session: async ({ session, token }) => {
if (session) {
session.user.id = token.id
session.user.name = token.name
session.user.email = token.email
session.user.roles = token.roles
}
return session
}
}
}
18 changes: 18 additions & 0 deletions lib/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrismaClient } from "@prisma/client"

declare global {
// eslint-disable-next-line no-var
var cachedPrisma: PrismaClient
}

let prisma: PrismaClient
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient()
} else {
if (!global.cachedPrisma) {
global.cachedPrisma = new PrismaClient()
}
prisma = global.cachedPrisma
}

export const db = prisma
6 changes: 6 additions & 0 deletions lib/stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Stripe from 'stripe'

export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
apiVersion: "2022-11-15",
typescript: true
})
46 changes: 46 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { getToken } from "next-auth/jwt"
import { withAuth } from "next-auth/middleware"
import { NextResponse } from "next/server"

export default withAuth(
async function middleware(req) {
const token = await getToken({ req })
const isAuth = !!token
console.log("isauth===>",isAuth)
const isAuthPage =
req.nextUrl.pathname.startsWith("/auth/login") ||
req.nextUrl.pathname.startsWith("/auth/register")

if (isAuthPage) {
if (isAuth) {
return NextResponse.redirect(new URL("/", req.url))
}
return null
}

if (!isAuth) {
let from = req.nextUrl.pathname;
if (req.nextUrl.search) {
from += req.nextUrl.search;
}

return NextResponse.redirect(
new URL(`/auth/login?from=${encodeURIComponent(from)}`, req.url)
);
}
},
{
callbacks: {
async authorized() {
// This is a work-around for handling redirect on auth pages.
// We return true here so that the middleware function above
// is always called.
return true
},
},
}
)

export const config = {
matcher: ["/:path*", "/auth/login", "/auth/register"],
}
6 changes: 6 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
}

module.exports = nextConfig
Loading

0 comments on commit 1460079

Please sign in to comment.