class UserService {
signIn(email, password) {
...
}
makeAdmin(user) {
...
}
...
Yep, although if I were to 'modularise' my application, I _probably_ wouldn't use static methods in my services (I know in Java, static methods can be unwieldy when testing your application.. not sure about ES6; I reckon it must be a similar situation there?)You must be registered for see links
PHP:import JWT from '~/app/lib/jwt' import Database from '~/app/sql/interactors/session' export default class SessionService { static create(user) { return new Promise(async (resolve, reject) => { let session = {} session = await Database.create(user) session = await JWT.sign(session) resolve(session) }) } static read(session) { return new Promise(async (resolve, reject) => { // Validate JWT session = await JWT.validate(session) // Check Session State (Must be "allowed") and return user session = await Database.read(session.id, true) if (session) { resolve({session: session.id, user: session.user}) } else { reject() } }) } static delete(session) { return Database.delete(session) } }
You must be registered for see links
PHP:import Database from '~/app/sql/interactors/user' export default class UserService { static login(username, password) { return new Promise(async (resolve, reject) => { let user = {} // Fetch User user = await Database.read(username, 'private') await user.verifyPassword(password) resolve(user) }) } static register(username, password, mail, ip) { return new Promise(async (resolve, reject) => { try { // Create User user = await Database.create({username: username, password: password, mail: mail, ip: ip}) resolve(user) } catch (error) { reject(error) } }) } static update(user) { return new Promise((resolve, reject) => {}) } }
So this, essentially?
I don't get the way you're using async / await.You must be registered for see links
PHP:import JWT from '~/app/lib/jwt' import Database from '~/app/sql/interactors/session' export default class SessionService { static create(user) { return new Promise(async (resolve, reject) => { let session = {} session = await Database.create(user) session = await JWT.sign(session) resolve(session) }) } static read(session) { return new Promise(async (resolve, reject) => { // Validate JWT session = await JWT.validate(session) // Check Session State (Must be "allowed") and return user session = await Database.read(session.id, true) if (session) { resolve({session: session.id, user: session.user}) } else { reject() } }) } static delete(session) { return Database.delete(session) } }
You must be registered for see links
PHP:import Database from '~/app/sql/interactors/user' export default class UserService { static login(username, password) { return new Promise(async (resolve, reject) => { let user = {} // Fetch User user = await Database.read(username, 'private') await user.verifyPassword(password) resolve(user) }) } static register(username, password, mail, ip) { return new Promise(async (resolve, reject) => { try { // Create User user = await Database.create({username: username, password: password, mail: mail, ip: ip}) resolve(user) } catch (error) { reject(error) } }) } static update(user) { return new Promise((resolve, reject) => {}) } }
So this, essentially?
static async create(user) {
const session = await Database.create(user)
const token = await JWT.sign(session)
return token
}
static async read(session) {
const jwt = await JWT.validate(session)
const session = await Database.read(session.id, true)
if (!session) throw new Error('invalid session')
return { session: session.id, user: session.user }
}