A lightweight and simple library for passwordless authentication. Built to help you get things done quickly and securely
npm i simple-passwordless-auth
- Google OAuth2
- Passwordless authentication with a Drizzle ORM integration (you can also use your own database / ORM)
import { login, validateCode } from 'simple-passwordless-auth';
// Login with email
let authCode;
await login(
email,
(user) => {
console.log('getUserByEmailAndUpdateUserIfExist');
authCode = user.authCode;
console.log({ authCode });
return Promise.resolve({} as UserDao);
},
() => {
console.log('createUser');
return Promise.resolve();
},
() => {
console.log('sendEmailWithVerificationCode');
return Promise.resolve();
},
);
// Validate the auth code
const isValid = await validateCode(process.env.JWT_SECRET ?? '', email, authCode, () => {
console.log('getUserByEmail');
return Promise.resolve({} as UserDao);
});
import { initGoogleOAuth2Client } from 'simple-passwordless-auth';
const googleClient = initGoogleOAuth2Client(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.GOOGLE_REDIRECT_URL,
);
// Redirect user to googleAuthUrl
const googleAuthUrl = getGoogleAuthUrl(googleClient);
// Handle the callback in a separate route
const userInfo = await handleGoogleCallback(googleClient, code);
- Go to Google Cloud Console
- Create a new project
- Enable the Google OAuth2 API
- Init a new OAuth consent screen
- Create credentials for OAuth2 client ID and secret (copy the client ID and secret to
initGoogleOAuth2Client()
) - Set the redirect URL (example:
http://localhost:3000/auth/google/callback
)
See debug/main.ts for a complete example
See CONTRIBUTING.md for more information