1.0.2 • Published 8 months ago

passport-gitlab-ts v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

passport-gitlab-ts

GitLab OAuth2 authentication strategy for Passport, written in TypeScript.

Features

  • Easy integration with Passport.js for GitLab authentication.
  • Supports customizable GitLab base URL for self-hosted instances.
  • Written in TypeScript with full type definitions included.

Installation

Install the package via npm:

npm install passport-gitlab-ts

Usage

Basic Example

import passport from 'passport';
import { GitLabStrategy } from 'passport-gitlab-ts';

passport.use(
  new GitLabStrategy(
    {
      clientID: 'YOUR_GITLAB_CLIENT_ID',
      clientSecret: 'YOUR_GITLAB_CLIENT_SECRET',
      callbackURL: 'http://localhost:3000/auth/gitlab/callback',
    },
    (accessToken, refreshToken, profile, done) => {
      // Use the profile information to find or create a user in your database
      done(null, profile);
    },
  ),
);

// Express route handlers
app.get('/auth/gitlab', passport.authenticate('gitlab'));

app.get(
  '/auth/gitlab/callback',
  passport.authenticate('gitlab', { failureRedirect: '/' }),
  (req, res) => {
    // Successful authentication
    res.redirect('/');
  },
);

Configuration Options

OptionTypeDefaultDescription
clientIDstringRequiredGitLab application client ID.
clientSecretstringRequiredGitLab application client secret.
callbackURLstringRequiredThe URL to which GitLab will redirect after authentication.
baseUrlstringhttps://gitlab.comGitLab instance base URL (for self-hosted).
scopestring[]['read_user']Permissions your app needs.

Customizing the Strategy

For self-hosted GitLab instances, provide a custom baseUrl:

new GitLabStrategy(
  {
    clientID: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    callbackURL: 'http://localhost:3000/auth/gitlab/callback',
    baseUrl: 'https://gitlab.example.com',
  },
  (accessToken, refreshToken, profile, done) => {
    done(null, profile);
  },
);

Profile Format

The strategy returns a profile object with the following structure:

interface Profile {
  provider: 'gitlab';
  id: string;
  displayName: string;
  username: string;
  profileUrl: string;
  photos: Array<{ value: string }>;
  emails: Array<{ value: string }>;
  _json: any; // Raw GitLab API response
}

Testing

Run the test suite:

npm test

License

This project is licensed under the MIT License.


Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago