Last active
August 27, 2023 05:49
-
-
Save ManotLuijiu/aa68ff0d14d6b55acb97710d1e73aa5f to your computer and use it in GitHub Desktop.
sharing Auth among Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In protected routes of App A and App B | |
import { validateToken } from './path-to-shared-auth-service'; | |
// Middleware to check token before accessing protected routes | |
async function requireAuthentication(req, res, next) { | |
const token = req.cookies.authToken; // Retrieve token from cookies | |
const isValidToken = await validateToken(token); | |
if (isValidToken) { | |
// User is authenticated, proceed to protected content | |
next(); | |
} else { | |
// Token is invalid, redirect to login or handle as needed | |
res.redirect('/login'); | |
} | |
} | |
// Usage in a protected route | |
app.get('/protected-route', requireAuthentication, (req, res) => { | |
// Render protected content | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pages/api/auth/[...nextauth].js | |
import NextAuth from 'next-auth'; | |
import Providers from 'next-auth/providers'; | |
export default NextAuth({ | |
providers: [ | |
Providers.Google({ | |
clientId: process.env.GOOGLE_CLIENT_ID, | |
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | |
}), | |
// Add more providers as needed | |
], | |
// Add session management, token handling, etc. | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App B | |
// pages/api/auth/[...nextauth].js (similar to App A) | |
import NextAuth from 'next-auth'; | |
import Providers from 'next-auth/providers'; | |
export default NextAuth({ | |
providers: [ | |
Providers.Google({ | |
clientId: process.env.GOOGLE_CLIENT_ID, | |
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | |
}), | |
// Add more providers as needed | |
], | |
// Add session management, token handling, etc. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import React, { useState } from 'react';
function YourComponent() {
const numberOfItems = 5; // You can adjust this as needed
const initialActiveState = Array.from({ length: numberOfItems }, () => false);
const [active, setActive] = useState(initialActiveState);
// Your component JSX and logic here
}