This is a Next.js project bootstrapped with create-next-app
.
This repo containing the following packages: next
next-auth
firebase
@mui/material
mongodb
googleapis
- Run install packages
npm install
# or
yarn
- Create file .env.local at root path
NODE_ENV=development
MONGO_URI=mongodb://localhost:27017/admin
DB_NAME=your-db-name
TOKEN_SECRET=YOUR_JWT_SECRET
TOKEN_EXPIRES_IN=7d
TURNSTILE_SECRET=YOUR_TURNSTILE_SECRET
GOOGLE_ID=xxx.apps.googleusercontent.com
GOOGLE_SECRET=xxx
GOOGLE_REDIRECT_URI=http://localhost:3000/auth/spreadsheet/callback
FIREBASE_SERVICE_ACCOUNT_KEY={"type":"service_account", ...}
NEXTAUTH_URL=http://localhost:3000/
NEXTAUTH_SECRET=your-random-text
BASE_URL=http://localhost:3000
- Create file /config/dev.js
import { ConfigType } from 'types';
import { ServiceAccount } from 'types/auth';
import serviceAccountKey from '../serviceAccountKey.json';
const keys = Object.freeze({
NODE_ENV: 'development',
MONGO_URI: 'mongodb://localhost:27017/admin',
DB_NAME: 'dbName',
TOKEN_SECRET: process.env.JWT_SECRET || 'JWT_SECRET',
TOKEN_EXPIRES_IN: process.env.JWT_EXPIRE || '7d',
TURNSTILE_SECRET: process.env.TURNSTILE_SECRET || '',
GOOGLE_ID: process.env.GOOGLE_ID || '',
GOOGLE_SECRET: process.env.GOOGLE_SECRET || '',
GOOGLE_REDIRECT_URI: process.env.GOOGLE_REDIRECT_URI || '',
FIREBASE_SERVICE_ACCOUNT_KEY: serviceAccountKey as ServiceAccount,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET || '',
BASE_URL: process.env.BASE_URL,
});
export default keys as ConfigType;
You can following this tutorial for more clearify
NextAuthJs-V4 | Next Authentication With Firebase | ServerSide Rendering (Google Auth Provider)
- Go to Firebase console
- Click Add Project
- Enter project name
nextjs-tutorial
- Disable Google Analytics for this project
- Click Create project
- When it’s ready click Continue
- Go to menu Build -> Firebase Database
- Click Create database
- Select Start in production mode
- Select Cloud Firestore location
- Click Enable
- Go to tab Rules
- Change
allow read, write: if false;
toallow read, write: if true;
- Click Publish
- Go to Firebase Project settings
- Under section Your apps -> Click web icon
- App nickname =
your-project-name
- Click Register app
- Copy firebaseConfig =
{apiKey, authDomain, projectId, storageBucket, messagingSenderId, appId}
- Click Continue to console
- Go to menu Build -> Authentication
- Click Get started
- Go to tab Sign-in method Click Google -> Enable
- Project public-facing name =
nextjs-tutorial-auth
- Click Save button
- Click Edit on
Google
record - Expand Web SDK configuration
- Copy
Web client ID
andWeb client secret
- Hover mouse at
?
after Web SDK configuration - Click link Google API Console
- Under OAuth 2.0 Client IDs click
Web client (auto created by Google Service)
- Add following URL to
Authorized JavaScript origins
- Add following URLs to
Authorized redirect URIs
- Click SAVE
- Go to Firebase project settings > Service accounts > Firebase Admin SDK
- Click Generate new private key
- Save as
serviceAccountKey.json
in the root project
{
"type": "service_account",
"project_id": "nextjs-tutorial-xxx",
"private_key_id": "xxx",
"private_key": "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-xxx@nextjs-tutorial-xxx.iam.gserviceaccount.com",
"client_id": "123",
"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-xxx%40nextjs-tutorial-xxx.iam.gserviceaccount.com"
}
- Go to https://console.cloud.google.com
- Select your project
- Go to Library menu
- Enable
Drive API
&Google Sheets API
...
GOOGLE_ID= <-- paste from 4.8
GOOGLE_SECRET= <-- paste from 4.8
FIREBASE_SERVICE_ACCOUNT_KEY= <-- put content from 5.3 serviceAccountKey.json here when deploy production
...
- Run command
npm run dev
# or
yarn dev
Open http://localhost:3000 with your browser to see the result.
Go to tab User then Active your user (First active user will be assign as Admin)