You can easily allow users to log in to your app via email using the db.signIn
function. This is how you could add a simple log in feature in your app. Make sure you have enabled the auth
module. Here’s a code snippet to implement basic email sign in:
import { API } from 'space-api';
// Initialize api with the project name and url of the space cloud
const api = new API('todo_app', 'http://localhost:4122');
// Initialize database(s) you intend to use
const db = api.DB("mydb");
// SignIn
db.signIn('demo@example.com', '1234').then(res => {
if (res.status === 200) {
// Set the token id to enable operations of other modules
api.setToken(res.data.token)
// res.data contains request payload
console.log('Response:', res.data);
return;
}
// Request failed
}).catch(ex => {
// Exception occured while processing request
});
As you would have noticed, the above function is asynchronous in nature. The signIn
method takes 2 parameters:
On getting the log in request, Space Cloud validates whether such an user exists and sends a response accordingly. A response object sent by the server contains the status and data fields explained below.
status: Number describing the http status code of the response. Following values are possible:
data: The data object consists of the following fields:
The next step would be checking out the client reference to register a new user.