# login

This page provides examples of login operations.

## Prerequisites

See the Sign Up for more information.

### Dependent Libraries

Only need reference two Javascript Libraries in HTML:

```markup
<script src="https://TML.ink/X.tml"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
```

The first line reference the client function library. This's a dynamic library includes many dynamic components and CDN resources, so don't save to local. The second line is using third-party socket library from CDN.

### Create a client instance

```java
tim = TML('wss://x.TML.ink'); //Create a client instance
```

If you connect multiple servers at the same time, you can create multiple client instances. Connect a server, Load authentication module, Provide offline login codes and method, logout codes and method, multi-server login, automatic login, user cache and other functions of authentication. If you don't need these features, you should not execute this line of code.

### Log In code

```javascript
tim.Login({
     email: "contact@TML.ink",
     password: "123"
   });
```

Use local cache to automatically login can be written as follow:

```javascript
tim.Login();
```

### Logout code

```javascript
tim.Logout();
```

### Get user example

```javascript
console.log(await tim.X('user').Get());//Get top 10 users

//Get top 50 users
 console.log(await tim.X('user').Get({
   $max: 50
     }))

//Get the user whose email is 'a'
 console.log(await tim.X('user').Get({
   email: 'contact@TML.ink'
 }))
```

Above codes are equivalent to:

```javascript
tim.X('user').Get().then(data => console.log(data));//Get top 10 users

//Get top 50 users
  tim.X('user').Get({
      $max: 50
     }).then(data => console.log(data));

//Get the user whose email is 'a'
  tim.X('user').Get({
   email: 'contact@TML.ink'
     }).then(data => console.log(data));
```

## Catch Log In's exception

Use try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.

### Asynchronous catch

```javascript
tim.Login({
    email: "contact@TML.ink",
    password: "123"
  }).then(function(rs) {
    console.log(rs);
  })
  .catch(function(err) {
    console.log(err);
    console.log(err.message);
  })
```

### Synchronous catch

```javascript
try {
  await tim.Login({
    email: "contact@TML.ink",
    password: "123"
  });
} catch (err) {
  console.log(err);
  console.log(err.message);
}
```

## Complete codes of Log In

[import](https://github.com/NeoTim/Doc.tml.ink/tree/f02cd2c5a14a3fffc882bf1f4bd35d2e08f721c4/src/book/examples/login_code.html)
