Model Integration

DeviseTokenAuth::Concerns::User

Typical use of this gem will not require the use of any of the following model methods. All authentication should be handled invisibly by the controller concerns.
Models that include the DeviseTokenAuth::Concerns::User concern will have access to the following public methods (read the above section for context on token and client):
  • valid_token?: check if an authentication token is valid. Accepts a token and client as arguments. Returns a boolean.
    Example:
    1
    # extract token + client from auth header
    2
    client = request.headers['client']
    3
    token = request.headers['access-token']
    4
    5
    @resource.valid_token?(token, client)
    Copied!
  • create_new_auth_token: creates a new auth token with all of the necessary metadata. Accepts client as an optional argument. Will generate a new client if none is provided. Returns the authentication headers that should be sent by the client as an object.
    Example:
    1
    # extract client from auth header
    2
    client = request.headers['client']
    3
    4
    # update token, generate updated auth headers for response
    5
    new_auth_header = @resource.create_new_auth_token(client)
    6
    7
    # update response with the header that will be required by the next request
    8
    response.headers.merge!(new_auth_header)
    Copied!
  • build_auth_header: generates the auth header that should be sent to the client with the next request. Accepts token and client as arguments. Returns a string.
    Example:
    1
    # create token
    2
    token = DeviseTokenAuth::TokenFactory.create
    3
    4
    # store client + token in user's token hash
    5
    @resource.tokens[token.client] = {
    6
    token: token.token_hash,
    7
    expiry: token.expiry
    8
    }
    9
    10
    # generate auth headers for response
    11
    new_auth_header = @resource.build_auth_header(token.token, token.client)
    12
    13
    # update response with the header that will be required by the next request
    14
    response.headers.merge!(new_auth_header)
    Copied!
Copy link