1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00

Added session service

This commit is contained in:
Laurent Cozic 2017-05-07 23:20:34 +01:00
parent e5d7294b69
commit b6cf34450f
7 changed files with 57 additions and 23 deletions

View File

@ -7,24 +7,21 @@ import { Provider } from 'react-redux'
import { WebApi } from 'src/web-api.js' import { WebApi } from 'src/web-api.js'
import { Database } from 'src/database.js' import { Database } from 'src/database.js'
//import { Session } from 'src/model/session.js';
import { SessionService } from 'src/service/session-service.js';
import { Log } from 'src/log.js' import { Log } from 'src/log.js'
let debugMode = true; let debugMode = true;
let clientId = 'A7D301DA7D301DA7D301DA7D301DA7D3';
let db = new Database(); let db = new Database();
db.setDebugEnabled(debugMode); db.setDebugEnabled(debugMode);
db.open(); db.open();
// let test = {
// 'abcd' : 123,
// 'efgh' : 456,
// }
// for (let [key, value] of test) {
// console.info(key, value);
// }
let defaultState = { let defaultState = {
'myButtonLabel': 'clicko123456', 'myButtonLabel': 'clicko123456',
@ -129,16 +126,12 @@ class App extends Component {
} }
// let api = new WebApi('http://192.168.1.2', 'A7D301DA7D301DA7D301DA7D301DA7D3'); // let api = new WebApi('http://192.168.1.2');
// api.exec('POST', 'sessions', null, { // let sessionService = new SessionService(api);
// 'email': 'laurent@cozic.net', // sessionService.login('laurent@cozic.net', '12345678', clientId).then((session) => {
// 'password': '12345678',
// })
// .then(function(data) {
// console.info('GOT DATA:'); // console.info('GOT DATA:');
// console.info(data); // console.info(session);
// }) // }).catch(function(error) {
// .catch(function(error) {
// console.warn('GOT ERROR:'); // console.warn('GOT ERROR:');
// console.warn(error); // console.warn(error);
// }) // })

View File

@ -0,0 +1,5 @@
class BaseModel {
}
export { BaseModel };

View File

@ -0,0 +1,9 @@
class BaseService {
constructor(webApi) {
this.api_ = webApi;
}
}
export { BaseService };

View File

@ -0,0 +1,11 @@
import { BaseModel } from 'src/base-model.js';
class Session extends BaseModel {
static login(email, password) {
}
}
export { Session };

View File

@ -0,0 +1,15 @@
import { BaseService } from 'src/base-service.js';
class SessionService extends BaseService {
login(email, password, clientId) {
return this.api_.post('sessions', null, {
'email': email,
'password': password,
'client_id': clientId,
});
}
}
export { SessionService };

View File

@ -2,9 +2,8 @@ const queryString = require('query-string');
class WebApi { class WebApi {
constructor(baseUrl, clientId) { constructor(baseUrl) {
this.baseUrl_ = baseUrl; this.baseUrl_ = baseUrl;
this.clientId_ = clientId;
} }
makeRequest(method, path, query, data) { makeRequest(method, path, query, data) {
@ -36,11 +35,15 @@ class WebApi {
fetch(r.url, r.options).then(function(response) { fetch(r.url, r.options).then(function(response) {
let responseClone = response.clone(); let responseClone = response.clone();
return response.json().then(function(data) { return response.json().then(function(data) {
if (data && data.error) {
reject(data);
} else {
resolve(data); resolve(data);
}
}) })
.catch(function(error) { .catch(function(error) {
responseClone.text().then(function(text) { responseClone.text().then(function(text) {
reject('Cannot parse JSON: ' + text); reject(new Error('Cannot parse JSON: ' + text));
}); });
}); });
}) })

View File

@ -29,8 +29,6 @@ class Session extends BaseModel {
$ok = self::verifyPassword($password, $user->password); $ok = self::verifyPassword($password, $user->password);
if (!$ok) throw new AuthException(); if (!$ok) throw new AuthException();
if (!$clientId) throw new \Exception('clientId is required');
$session = new Session(); $session = new Session();
$session->owner_id = $user->id; $session->owner_id = $user->id;
$session->client_id = $clientId; $session->client_id = $clientId;