diff --git a/examples/social-network/app/controllers/users.js b/examples/social-network/app/controllers/users.js index 822978bb..d484e80e 100644 --- a/examples/social-network/app/controllers/users.js +++ b/examples/social-network/app/controllers/users.js @@ -1,11 +1,30 @@ import { Controller } from 'lux-framework'; +import User from 'app/models/user'; + class UsersController extends Controller { params = [ 'name', 'email', 'password' ]; + + async login({ + params: { + data: { + attributes: { + email, + password + } + } + } + }) { + const user = await User.findByEmail(email); + + if (user) { + return user.authenticate(password); + } + } } export default UsersController; diff --git a/examples/social-network/app/models/action.js b/examples/social-network/app/models/action.js index 17aa37ec..1aa3fbc3 100644 --- a/examples/social-network/app/models/action.js +++ b/examples/social-network/app/models/action.js @@ -1,10 +1,10 @@ import { Model } from 'lux-framework'; -import Comment from './comment'; -import Notification from './notification'; -import Post from './post'; -import Reaction from './reaction'; -import User from './user'; +import Comment from 'app/models/comment'; +import Notification from 'app/models/notification'; +import Post from 'app/models/post'; +import Reaction from 'app/models/reaction'; +import User from 'app/models/user'; /* TODO: Add support for polymorphic relationship to a 'trackable'. * https://github.com/postlight/lux/issues/75 diff --git a/examples/social-network/app/models/comment.js b/examples/social-network/app/models/comment.js index 0d5938f4..00bf2701 100644 --- a/examples/social-network/app/models/comment.js +++ b/examples/social-network/app/models/comment.js @@ -1,6 +1,6 @@ import { Model } from 'lux-framework'; -import track from '../utils/track'; +import track from 'app/utils/track'; class Comment extends Model { static belongsTo = { diff --git a/examples/social-network/app/models/post.js b/examples/social-network/app/models/post.js index ffc34f2c..2634adea 100644 --- a/examples/social-network/app/models/post.js +++ b/examples/social-network/app/models/post.js @@ -1,6 +1,6 @@ import { Model } from 'lux-framework'; -import track from '../utils/track'; +import track from 'app/utils/track'; class Post extends Model { static belongsTo = { diff --git a/examples/social-network/app/models/reaction.js b/examples/social-network/app/models/reaction.js index ee4d153a..208e8889 100644 --- a/examples/social-network/app/models/reaction.js +++ b/examples/social-network/app/models/reaction.js @@ -1,6 +1,6 @@ import { Model } from 'lux-framework'; -import track from '../utils/track'; +import track from 'app/utils/track'; class Reaction extends Model { static belongsTo = { diff --git a/examples/social-network/app/models/user.js b/examples/social-network/app/models/user.js index 9cae12ef..93414f2b 100644 --- a/examples/social-network/app/models/user.js +++ b/examples/social-network/app/models/user.js @@ -4,9 +4,7 @@ import { generateSalt, encryptPassword, decryptPassword -} from '../utils/password'; - -const { assign } = Object; +} from 'app/utils/password'; class User extends Model { static hasMany = { @@ -46,7 +44,7 @@ class User extends Model { if ((typeof id !== 'number') && password || dirtyAttributes.has('password')) { const salt = generateSalt(); - assign(user, { + Object.assign(user, { password: encryptPassword(password, salt), passwordSalt: salt }); @@ -54,31 +52,24 @@ class User extends Model { } }; + static scopes = { + findByEmail(email) { + return this.first().where({ + email + }); + } + }; + static validates = { password(password = '') { return password.length >= 8; } }; - static async authenticate(email, password) { - const user = await this.findOne({ - where: { - email - } - }); - - if (user) { - const { - password: encrypted, - passwordSalt: salt - } = user; - - if (password === decryptPassword(encrypted, salt)) { - return user; - } - } + authenticate(password) { + const { password: encrypted, passwordSalt: salt } = this; - return false; + return password === decryptPassword(encrypted, salt); } } diff --git a/examples/social-network/app/routes.js b/examples/social-network/app/routes.js index bbc314f2..2ff69acc 100644 --- a/examples/social-network/app/routes.js +++ b/examples/social-network/app/routes.js @@ -15,6 +15,11 @@ export default function routes() { action: 'show' }); + this.route('friendships', { + method: 'POST', + action: 'create' + }); + this.route('notifications', { method: 'GET', action: 'index' @@ -24,4 +29,9 @@ export default function routes() { method: 'GET', action: 'show' }); + + this.route('users/login', { + method: 'POST', + action: 'login' + }); } diff --git a/examples/social-network/app/serializers/comments.js b/examples/social-network/app/serializers/comments.js index 5542a660..123eb663 100644 --- a/examples/social-network/app/serializers/comments.js +++ b/examples/social-network/app/serializers/comments.js @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework'; class CommentsSerializer extends Serializer { attributes = [ 'edited', - 'message' + 'message', + 'createdAt', + 'updatedAt' ]; hasOne = [ diff --git a/examples/social-network/app/serializers/friendships.js b/examples/social-network/app/serializers/friendships.js index 5cae8884..10fd9ada 100644 --- a/examples/social-network/app/serializers/friendships.js +++ b/examples/social-network/app/serializers/friendships.js @@ -1,9 +1,11 @@ import { Serializer } from 'lux-framework'; class FriendshipsSerializer extends Serializer { - hasOne = [ - 'followee', - 'follower' + attributes = [ + 'followerId', + 'followeeId', + 'createdAt', + 'updatedAt' ]; } diff --git a/examples/social-network/app/serializers/notifications.js b/examples/social-network/app/serializers/notifications.js index 65bd2ad2..5aaaed54 100644 --- a/examples/social-network/app/serializers/notifications.js +++ b/examples/social-network/app/serializers/notifications.js @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework'; class NotificationsSerializer extends Serializer { attributes = [ 'unread', - 'message' + 'message', + 'createdAt', + 'updatedAt' ]; hasOne = [ diff --git a/examples/social-network/app/serializers/posts.js b/examples/social-network/app/serializers/posts.js index 33581591..b8205629 100644 --- a/examples/social-network/app/serializers/posts.js +++ b/examples/social-network/app/serializers/posts.js @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework'; class PostsSerializer extends Serializer { attributes = [ 'body', - 'title' + 'title', + 'createdAt', + 'updatedAt' ]; hasOne = [ diff --git a/examples/social-network/app/serializers/reactions.js b/examples/social-network/app/serializers/reactions.js index 69742ce0..2062934b 100644 --- a/examples/social-network/app/serializers/reactions.js +++ b/examples/social-network/app/serializers/reactions.js @@ -2,7 +2,8 @@ import { Serializer } from 'lux-framework'; class ReactionsSerializer extends Serializer { attributes = [ - 'type' + 'type', + 'createdAt' ]; hasOne = [ diff --git a/examples/social-network/app/utils/password.js b/examples/social-network/app/utils/password.js index 0b11fe58..7821fba4 100644 --- a/examples/social-network/app/utils/password.js +++ b/examples/social-network/app/utils/password.js @@ -21,9 +21,3 @@ export function decryptPassword(hash, secret) { decrypted += decipher.final('utf8'); return decrypted; } - -export default { - generateSalt, - encryptPassword, - decryptPassword -}; diff --git a/examples/social-network/app/utils/track.js b/examples/social-network/app/utils/track.js index 16f765c9..c5ad9e9b 100644 --- a/examples/social-network/app/utils/track.js +++ b/examples/social-network/app/utils/track.js @@ -1,4 +1,4 @@ -import Action from '../models/action'; +import Action from 'app/models/action'; export default async function track(trackable) { if (trackable) { diff --git a/examples/social-network/package.json b/examples/social-network/package.json index 943d87f0..75e91096 100644 --- a/examples/social-network/package.json +++ b/examples/social-network/package.json @@ -10,9 +10,9 @@ "author": "", "license": "MIT", "dependencies": { - "babel-core": "6.11.4", + "babel-core": "6.13.2", "babel-preset-lux": "1.1.0", - "knex": "0.11.9", + "knex": "0.11.10", "lux-framework": "1.0.0-rc.5", "sqlite3": "3.1.4" }, diff --git a/examples/todo/package.json b/examples/todo/package.json index 597d406d..85339838 100644 --- a/examples/todo/package.json +++ b/examples/todo/package.json @@ -10,9 +10,9 @@ "author": "", "license": "MIT", "dependencies": { - "babel-core": "6.11.4", + "babel-core": "6.13.2", "babel-preset-lux": "1.1.0", - "knex": "0.11.9", + "knex": "0.11.10", "lux-framework": "1.0.0-rc.5", "sqlite3": "3.1.4" },