a Sails application
Getting up and running is as easy as 1, 2, 3....
-
Install your dependencies
cd path/to/api-bootcamp; npm install
-
Install node foreman globally
npm install -g foreman
-
Full fill .env file, set MONGOURL, you may create account on mlab.com, it is free. Url should be like
mongodb://<user name>:<password>@<db host url>:db host port/<db name>
-
Start your app
nf start
##Methods
/signin
requireslogin
andpassword
, will check against db and if user exists, will return object with JWT and user data
{
token:'JWT here',
data:{
id:'userId',
login:'userLogin',
createdAt:'',
updatedAt:''
}
}
/signup
acceptslogin
,password
,confirmation
returns same response as/signin
- in order to get access to other methods we should add this JWT token to requests headers
{Authorization: `Bearer ${jwt}`}
- write courses api
Cannot promise backward compatibility, so be ready to drop your DB and fill it again
FYI I am lazy person, and do not promise to do anything in time, I do it only for myself and if it could help for some of you, you can always say Thanks, but the best thanks is PR with something useful!
to be able signup/signin
with facebook profile, you need to create facebook app, add facebook login product to the app, make it public, get facebook app id and secret key and add them to .env file.
next step: you need to implement frontend facebook login, it is easy to do with FBConector there is an example and the lib
My code example of fb login
fbLogin(){
return Observable.create((observer)=>{
let accessToken;
FB.login((response:any)=>{
if (response.status === 'connected'){
accessToken = response.authResponse.accessToken;
return FB.api('/me','GET',(response:any)=>observer.next({login: response.name, token:accessToken, userId: response.id}))
}
return observer.error();
})
})
.flatMap(res => this.api.post(`/fbLogin`, res))
.map(res => res.data);
}
as you can see when you have an user, you need to send our backend /fbLogin
an object
{login: 'user name', token:'fb access token', userId: 'fb user id'}
backend will check this information against facebook, and if your token is valid, you will get standard signin/signup
response
{
token:'JWT here',
data:{
id:'userId',
login:'userLogin',
createdAt:'',
updatedAt:''
}
}
Copyright (c) 2016
Licensed under the MIT license.