Skip to content

Instantly share code, notes, and snippets.

@ManotLuijiu
Last active August 27, 2023 05:49
Show Gist options
  • Save ManotLuijiu/aa68ff0d14d6b55acb97710d1e73aa5f to your computer and use it in GitHub Desktop.
Save ManotLuijiu/aa68ff0d14d6b55acb97710d1e73aa5f to your computer and use it in GitHub Desktop.
sharing Auth among Apps
// 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
});
// 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.
});
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.
});
// In a shared authentication service or utility
import { getSession } from 'next-auth/react';
// Validate the incoming token from each app
async function validateToken(token) {
const session = await getSession({ token });
return session ? true : false;
}
export { validateToken };
@ManotLuijiu
Copy link
Author

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment