- Preparing search index...
- The search index is not available
git-essentials
Auth
Callback
: ((url
: string, auth
: Auth) => Auth | void | Promise<Auth | void>)
Type declaration
-
- (url: string, auth: Auth): Auth | void | Promise<Auth | void>
-
Returns Auth | void | Promise<Auth | void>
The callback allows to request credentials.
Authentication is normally required for pushing to a git repository. It may also be required to clone or fetch from a private repository. Git does all its authentication using HTTPS Basic Authentication.
Example
Option 1: Username & Password
Return an object with
{ username, password }
.However, there are some things to watch out for.
If you have two-factor authentication (2FA) enabled on your account, you probably cannot push or pull using your regular username and password. Instead, you may have to use a Personal Access Token.
Personal Access Tokens
In this situation, you want to return an object with
{ username, password }
wherepassword
is the Personal Access Token. Note that GitHub actually lets you specify the token as theusername
and leave the password blank, which is convenient but none of the other hosting providers do this that I'm aware of.OAuth2 Tokens
If you are writing a third-party app that interacts with GitHub/GitLab/Bitbucket, you may be obtaining OAuth2 tokens from the service via a feature like "Login with GitHub". Depending on the OAuth2 token's grants, you can use those tokens for pushing and pulling from git repos as well.
In this situation, you want to return an object with
{ username, password }
whereusername
andpassword
depend on where the repo is hosted.Option 2: Headers
This is the super flexible option. Just return the HTTP headers you want to add as an object with
{ headers }
. If you can provide{ username, password, headers }
if you want. (Although ifheaders
includes anAuthentication
property that overwrites what you would normally get fromusername
/password
.)To re-implement the default Basic Auth behavior, do something like this:
If you are using a custom proxy server that has its own authentication in addition to the destination authentication, you could inject it like so: