# Module: auth

Source:
  - auth.js, line 2

### Methods

#### (protected, static) authenticate(credentials, callback)

Authenticate with the server

You should use module:resin.auth.login when possible, as it takes care of saving the token and username as well. Notice that if `credentials` contains extra keys, they'll be discarted by the server automatically.

##### Parameters:

Name | Type | Description
--- | --- | ---
`credentials` | Object | in the form of username, password ###### Properties
Name | Type | Description
--- | --- | ---
`username` | String | the username
`password` | String | the password
`callback` | module:resin.auth~authenticateCallback | callback

Source:
  - auth.js, line 87

##### Example

    resin.auth.authenticate credentials, (error, token, username) ->
    		throw error if error?
    		console.log("My username is: #{username}")
    		console.log("My token is: #{token}")

#### (static) getToken()

Get current logged in user's token

This will only work if you used module:resin.auth.login to log in.

Source:
  - auth.js, line 207

##### Example

    token = resin.auth.getToken()
    	console.log(token)

#### (static) isLoggedIn(callback)

Check if you're logged in

##### Parameters:

Name | Type | Description
--- | --- | ---
`callback` | module:resin.auth~isLoggedInCallback | callback

Source:
  - auth.js, line 190

##### Example

    resin.auth.isLoggedIn (isLoggedIn) ->
    		if isLoggedIn
    			console.log('I\'m in!')
    		else
    			console.log('Too bad!')

#### (static) login(credentials, callback)

Login to Resin.io

If the login is successful, the token is persisted between sessions.

##### Parameters:

Name | Type | Description
--- | --- | ---
`credentials` | Object | in the form of username, password ###### Properties
Name | Type | Description
--- | --- | ---
`username` | String | the username
`password` | String | the password
`callback` | module:resin.auth~loginCallback | callback

Source:
  - auth.js, line 128

##### Example

    resin.auth.login credentials, (error) ->
    		throw error if error?
    		console.log('I\'m logged in!')

#### (static) loginWithToken(token, callback)

Login to Resin.io with a token

Login to resin with a session token instead of with credentials.

##### Parameters:

Name | Type | Description
--- | --- | ---
`token` | String | the auth token
`callback` | module:resin.auth~loginWithTokenCallback | callback

Source:
  - auth.js, line 162

##### Example

    resin.auth.loginWithToken token, (error) ->
    		throw error if error?
    		console.log('I\'m logged in!')

#### (static) logout(callbackopt)

Logout from Resin.io

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`callback` | module:resin.auth~logoutCallback | <optional> | _.noop | callback

Source:
  - auth.js, line 232
To Do:
  - Maybe we should post to /logout or something to invalidate the token on the server?

##### Example

    resin.auth.logout (error) ->
    		throw error if error?
    		console.log('I\'m out!')

#### (static) register(credentialsopt, callback)

Register to Resin.io

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`credentials` | Object | <optional> | {} | in the form of username, password and email ###### Properties
Name | Type | Description
--- | --- | ---
`username` | String | the username
`password` | String | the password
`email` | String | the email
`callback` | module:resin.auth~registerCallback |  |  | callback

Source:
  - auth.js, line 267

##### Example

    resin.auth.register {
    		username: 'johndoe'
    		password: 'secret'
    		email: 'johndoe@gmail.com'
    	}, (error, token) ->
    		throw error if error?
    		console.log(token)

#### (static) whoami(callback)

Return current logged in username

This will only work if you used module:resin.auth.login to log in.

##### Parameters:

Name | Type | Description
--- | --- | ---
`callback` | module:resin.auth~whoamiCallback | callback

Source:
  - auth.js, line 43

##### Example

    resin.auth.whoami (error, username) ->
    		throw error if error?

    		if not username?
    			console.log('I\'m not logged in!')
    		else
    			console.log("My username is: #{username}")