# Class Login

java.lang.Object
com.codename1.social.Login

Direct Known Subclasses:
  `AppleSignIn, Auth0Connect, FacebookConnect, GoogleConnect, MicrosoftConnect`

public abstract class Login extends Object
The Login abstract base class is used to simplify Oauth2 authentications services. Services can override the default Oauth2 web login and offers the native login experience.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Login()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Login`
  `addScopes(String... scopes)`
  Adds the given scopes to the OAuth2 login request.
  `AsyncResource<Login>`
  `connect()`
  Connects to the login service asynchronously, automatically logging in if not yet logged in.
  `protected Oauth2`
  `createOauth2()`
  Creates the oauth2 to be used to login in case no native login is available for this service.
  `void`
  `doLogin()`
  Logs in the user.
  `void`
  `doLogin(LoginCallback callback)`
  Initiates login using the given single-use callback.
  `void`
  `doLogout()`
  Logs out the current user
  `AccessToken`
  `getAccessToken()`
  The AccessToken of this service
  `abstract boolean`
  `isNativeLoginSupported()`
  Returns true if this service supports native login.
  `boolean`
  `isPreferRedirectPrompt()`
  A flag used by the javascript port to indicate that the login will use a redirect for the prompt instead of a popup.
  `boolean`
  `isUserLoggedIn()`
  Indicates if the user is currently logged in
  `boolean`
  `nativeIsLoggedIn()`
  Indicates if the user is currently logged in.
  `void`
  `nativelogin()`
  Logs in the current user natively.
  `void`
  `nativeLogout()`
  Logs out the current user natively.
  `void`
  `setAccessToken(AccessToken token)`
  Sets the Login access token
  `void`
  `setCallback(LoginCallback lc)`
  Sets the login callback that will receive event callback notification from the API
  `void`
  `setClientId(String id)`
  The client id (appid) which asks to connect
  `void`
  `setClientSecret(String secret)`
  The client secret
  `void`
  `setOauth2URL(String oauth2URL)`
  The oauth2 URL
  `void`
  `setPreferRedirectPrompt(boolean preferRedirectPrompt)`
  A flag used by the javascript port to indicate that the login will use a redirect for the prompt instead of a popup.
  `void`
  `setRedirectURI(String redirectURI)`
  The redirect URI
  `void`
  `setScope(String scope)`
  The authentication scope
  `void`
  `validateToken()`
  This method tries to validate the last access token if exists, if the last token is not valid anymore it will try to login the user in order to get a fresh token The method blocks until a valid token has been granted
  `protected abstract boolean`
  `validateToken(String token)`
  Returns true if the previous granted access token is still valid otherwise false.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Login

    public Login()

- ## Method Details

  - ### addScopes

    public Login addScopes(String... scopes)

    Adds the given scopes to the OAuth2 login request.

    ###### Parameters

    - `scopes`: Scopes to add.

    ###### Returns

    Self for chaining.

    ###### See also

    - #setScope(java.lang.String)

  - ### connect

    public AsyncResource<Login> connect()

    Connects to the login service asynchronously, automatically logging in if not yet logged in.

    ###### Returns

    AsyncResource that can be monitored for completion.

  - ### doLogin

    public void doLogin(LoginCallback callback)

    Initiates login using the given single-use callback.

    ###### Parameters

    - `callback`: Callback to be called if login succeeds or fails.

  - ### doLogin

    public void doLogin()
    Logs in the user. If the service has a native login it will try to use that, otherwise an Oauth2 web login will be used.

  - ### doLogout

    public void doLogout()
    Logs out the current user

  - ### isUserLoggedIn

    public boolean isUserLoggedIn()

    Indicates if the user is currently logged in

    ###### Returns

    true if logged in

  - ### nativeIsLoggedIn

    public boolean nativeIsLoggedIn()

    Indicates if the user is currently logged in. Subclasses that uses a native sdk to login/logout should override this method.

    ###### Returns

    true if logged in

  - ### nativelogin

    public void nativelogin()
    Logs in the current user natively. Subclasses that uses a native sdk to login/logout should override this method.

  - ### nativeLogout

    public void nativeLogout()
    Logs out the current user natively. Subclasses that uses a native sdk to login/logout should override this method.

  - ### isNativeLoginSupported

    public abstract boolean isNativeLoginSupported()

    Returns true if this service supports native login. If implementation returns true here, the nativelogin, nativelogout, nativeIsLoggedIn should be implemented

    ###### Returns

    true if the service supports native login

  - ### getAccessToken

    public AccessToken getAccessToken()

    The AccessToken of this service

    ###### Returns

    the token

  - ### setAccessToken

    public void setAccessToken(AccessToken token)
    Sets the Login access token

  - ### validateToken

    public void validateToken() throws IOException
    This method tries to validate the last access token if exists, if the last token is not valid anymore it will try to login the user in order to get a fresh token The method blocks until a valid token has been granted
    Throws:
      `IOException`

  - ### validateToken

    protected abstract boolean validateToken(String token)

    Returns true if the previous granted access token is still valid otherwise false.

    ###### Parameters

    - `token`: the access token to check

    ###### Returns

    true of the token is valid

  - ### setCallback

    public void setCallback(LoginCallback lc)

    Sets the login callback that will receive event callback notification from the API

    ###### Parameters

    - `lc`: @param lc the login callback or null to remove the existing login callback

  - ### setClientId

    public void setClientId(String id)

    The client id (appid) which asks to connect

    ###### Parameters

    - `clientId`

  - ### setClientSecret

    public void setClientSecret(String secret)

    The client secret

    ###### Parameters

    - `clientSecret`

  - ### setRedirectURI

    public void setRedirectURI(String redirectURI)

    The redirect URI

    ###### Parameters

    - `redirectURI`

  - ### setScope

    public void setScope(String scope)
    The authentication scope

  - ### setOauth2URL

    public void setOauth2URL(String oauth2URL)
    The oauth2 URL

  - ### createOauth2

    protected Oauth2 createOauth2()

    Creates the oauth2 to be used to login in case no native login is available for this service.

    ###### Returns

    Returns:
      the Oauth2 to be used to login if no native login available and on the simulator

  - ### isPreferRedirectPrompt

    public boolean isPreferRedirectPrompt()

    A flag used by the javascript port to indicate that the login will use a redirect for the prompt instead of a popup. On the web, a redirect is usually better UX but it can be problematic since it involves leaving the app, and reloading it after the login.

    ###### Returns

    the preferRedirectPrompt

  - ### setPreferRedirectPrompt

    public void setPreferRedirectPrompt(boolean preferRedirectPrompt)

    A flag used by the javascript port to indicate that the login will use a redirect for the prompt instead of a popup. On the web, a redirect is usually better UX but it can be problematic since it involves leaving the app, and reloading it after the login.

    ###### Parameters

    - `preferRedirectPrompt`: the preferRedirectPrompt to set