Skip to content

Commit

Permalink
[#2] [Backend] As a user I can login with an OAuth provider (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierobert committed Sep 27, 2022
1 parent 2c3efa1 commit 4d014fd
Show file tree
Hide file tree
Showing 38 changed files with 13,925 additions and 2,451 deletions.
1 change: 1 addition & 0 deletions nextjs/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="postgresql://postgres:@localhost:5432/next-newsletter_dev?schema=public"
3 changes: 3 additions & 0 deletions nextjs/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
COOKIE_SECRET=
OAUTH_GOOGLE_CLIENT_ID=
OAUTH_GOOGLE_CLIENT_SECRET=
11 changes: 10 additions & 1 deletion nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"extends": "next/core-web-vitals"
"extends": [
"next/core-web-vitals",
"@nimblehq/eslint-config-nimble",
"@nimblehq/eslint-config-nimble-react"
],

"rules": {
// suppress errors for missing 'import React' in files since Next.JS import React
"react/react-in-jsx-scope": "off"
}
}
3 changes: 3 additions & 0 deletions nextjs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# next.js
/.next/
/out/
.swc

# production
/build
Expand All @@ -26,7 +27,9 @@ yarn-error.log*
.pnpm-debug.log*

# local env files
.env
.env*.local
.env.test

# vercel
.vercel
Expand Down
7 changes: 7 additions & 0 deletions nextjs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: start, start_database

start: start_database
npm run dev

start_database:
docker-compose -f docker-compose.dev.yml up -d
35 changes: 25 additions & 10 deletions nextjs/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
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).
NextNewsletter 🚀 is bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Getting Started

First, run the development server:
### Prerequisites

```bash
npm run dev
# or
yarn dev
- Ensure you have ![node-version-image](https://img.shields.io/badge/node-16.17.0-brightgreen.svg) version installed.
- Define the local environment variables by copying `.env.sample`:

```
cp .env.sample .env.local
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
💡Secrets can be generated via node CLI with `crypto.randomBytes(128).toString('base64')` .

### Development

1. Install all dependencies:

```bash
npm install
# or
yarn install
```

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

[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`.
```bash
npm run dev
# or
yarn dev
```

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.
3. Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
11 changes: 11 additions & 0 deletions nextjs/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.8"

services:
db:
image: postgres:14.5
container_name: next-newsletter_db
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
- POSTGRES_DB=next-newsletter_dev
ports:
- "5432:5432"
26 changes: 26 additions & 0 deletions nextjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// eslint-disable-next-line
const nextJest = require('next/jest');

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})

// Add any custom config to be passed to Jest
const customJestConfig = {
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['<rootDir>/test/database.ts'],
collectCoverageFrom: [
'src/**/*.{js,jsx,ts,tsx}',
'!src/config/*.{js,ts}',
'!src/**/*.d.ts',
'!src/tests/**/*'
]
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);
15 changes: 15 additions & 0 deletions nextjs/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getIronSession } from 'iron-session/edge';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

import { sessionOptions } from './src/config/session';

export const middleware = async (req: NextRequest) => {
const res = NextResponse.next();
const session = await getIronSession(req, res, sessionOptions);
const { user } = session;

console.log('nextjs middleware', user);

return res;
};

0 comments on commit 4d014fd

Please sign in to comment.