auth Documentation

  1. API keys
  2. Register and login
  3. Users
  4. User data
  5. Code examples

API keys

Request a personal API key.

An API key is required for all requests.

If an API key is not provided the following error is given:

{
            "errors": {
            "status": 401,
            "source": "/products",
            "title": "Valid API key",
            "detail": "No valid API key provided."
        }
}

Register and login

Register

POST /register

Required parameters:

api_key
email
password

Result:

{
    "data": {
        "message": "User successfully registered."
    }
}

Login

POST /login

Required parameters:

api_key
email
password

Result:

{
    "data": {
        "type": "success",
        "message": "User logged in",
        "user": {
            "api_key": "...",
            "email": "unknown@example.com"
        },
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey..."
    }
}

N.B. The access token expires after 24 hours.

Users

Get all users

GET /users?api_key=[API_KEY]

Result:

{
    "data": [
        {
            "user_id": 1,
            "email": "test@test.se"
        }
    ]
}

Get specific user

GET /users/1?api_key=[API_KEY]

Result:

{
    "data": {
        "user_id": 1,
        "email": "test@test.se"
    }
}

User data

To use the following route, you need a valid JSON Web Token (JWT) set in the HTTP-header.
The 'x-access-token' header should contain the JWT.

Get all data for authenticated user

GET /data?api_key=[API_KEY]

User data has the following attributes:

id
artefact
api_key

In the following examples the artefact entity is used to store JSON-data. But any type of data can be stored for instance URLs, likes, upvotes or anything that can be stored as text.

Result:

{
    data: [
        {
            id: 1,
            email: 'test@example.com',
            artefact: '{"latitude":56.18185835,"longitude":15.5911037,"place":"BTH"}'
        }
    ]
}

Create data for authenticated user

POST /data

Required params:

artefact
api_key

Result:

{
    data:  {
        id: 1,
        email: 'test@example.com',
        artefact: '{"latitude":56.18185835,"longitude":15.5911037,"place":"BTH"}'
    }
}

Update data for authenticated user

PUT /data

Required params:

id
artefact
api_key

Result:

204 No Content

Delete data for authenticated user

PUT /data

Required params:

id
api_key

Result:

204 No Content

Code Examples

JavaScript fetch

POST /register
    var user = {
    email: "test@test.se",
    password: "test1234",
    api_key: [API_KEY]
};

fetch("https://auth.emilfolino.se/register", {
    body: JSON.stringify(user),
    headers: {
        'content-type': 'application/json'
    },
    method: 'POST'
})
.then(function (response) {
    return response.json();
}).then(function(result) {

});

POST /login
        var user = {
    email: "test@test.se",
    password: "test1234",
    api_key: [API_KEY]
};

fetch("https://auth.emilfolino.se/login", {
    body: JSON.stringify(user),
    headers: {
        'content-type': 'application/json'
    },
    method: 'POST'
})
.then(function (response) {
    return response.json();
}).then(function(result) {
    var token = result.data.token;
});

GET /users
            fetch("https://auth.emilfolino.se/users?api_key=[API_KEY]")
.then(function (response) {
    return response.json();
}).then(function(result) {
    var users = result.data;
});

GET /data
            fetch("https://auth.emilfolino.se/data?api_key=[API_KEY]", {
    headers: {
      'x-access-token': [TOKEN]
    }
})
.then(function (response) {
    return response.json();
}).then(function(result) {
    var allData = result.data;
});

POST /data
            var data = {
    artefact: '{"latitude":56.18185835,"longitude":15.5911037,"place":"BTH"}',
    api_key: [API_KEY]
};

fetch("https://auth.emilfolino.se/data", {
    body: JSON.stringify(data),
    headers: {
        'content-type': 'application/json',
        'x-access-token': [TOKEN]
    },
    method: 'POST'
})
.then(function (response) {
    return response.json();
}).then(function(result) {

});

Mithril

POST /register
    var user = {
    email: "test@test.se",
    password: "test1234",
    api_key: [API_KEY]
};

m.request({
    url: "https://auth.emilfolino.se/register",
    data: user,
    method: 'POST'
}).then(function(result) {

});

POST /login
        var user = {
    email: "test@test.se",
    password: "test1234",
    api_key: [API_KEY]
};

m.request({
    url: "https://auth.emilfolino.se/login",
    data: user,
    method: 'POST'
}).then(function(result) {
    var token = result.data.token;
});

GET /users
            m.request({
    method: "GET",
    url: "https://auth.emilfolino.se/users?api_key=[API_KEY]"
}).then(function(result) {
    var users = result.data;
});

GET /data
            m.request({
    method: "GET",
    url: "https://auth.emilfolino.se/data?api_key=[API_KEY]",
    headers: {
      'x-access-token': [TOKEN]
    }
}).then(function(result) {
    var allData = result.data;
});

POST /data
            var data = {
    artefact: '{"latitude":56.18185835,"longitude":15.5911037,"place":"BTH"}',
    api_key: [API_KEY]
};

m.request({
    url: "https://auth.emilfolino.se/login",
    data: user,
    method: 'POST',
    headers: {
      'x-access-token': [TOKEN]
    }
}).then(function(result) {

});