Skip to content

Lab-teddy #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lab-teddy/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
25 changes: 25 additions & 0 deletions lab-teddy/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"rules": {
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"no-console": "off",
"indent": [ "error", 2 ],
"semi": ["error", "always"]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"globals": {
"__API_URI__": false,
"__DEBUG__": false
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
2 changes: 2 additions & 0 deletions lab-teddy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
/build
1 change: 1 addition & 0 deletions lab-teddy/app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API_URL= http://localhost:3000
Empty file.
33 changes: 33 additions & 0 deletions lab-teddy/app/component/landing/login/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<section class="login-form">
<form
name="loginForm"
ng-submit="loginCtrl.login()" novalidate>
<div
ng-class="{
'error': loginForm.username.$invalid,
'success': loginForm.username.$valid,
}">
<input type="text"
name="username"
placeholder="username"
ng-minlength="4"
ng-model="loginCtrl.user.username" required>

<input type="text" placeholder="email">
</div>

<div
ng-class="{
'error': loginForm.password.$invalid && loginForm.$submitted,
'success': loginForm.password.$valid,
}">
<input type="password" name="password"
ng-disabled="loginForm.username.$invalid"
placeholder="password"
ng-minlength="3"
ng-model="loginCtrl.user.password" required>
</div>

<button class="btn-std">sign in</button>
</form>
</section>
21 changes: 21 additions & 0 deletions lab-teddy/app/component/landing/login/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('./_login.scss');

module.exports = {
template: require('./login.html'),
controller: ['$log', '$location', 'authService', LoginController],
controllerAs: 'loginCtrl',
};

function LoginController($log, $location, authService) {
$log.debug('LoginController');

authService.getToken().then(() => $location.url('/home'));

this.login = function() {
$log.log('loginCtrl.login()');

authService.login(this.user).then(() => $location.url('/home'));
};
}
Empty file.
27 changes: 27 additions & 0 deletions lab-teddy/app/component/landing/signup/signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<section class="signup">
<form class="signup-form"
ng-submit="signupCtrl.signup(signupCtrl.user)"
novalidate>

<input class="input-std"
type="text"
placeholder="name"
ng-minlength="4"
ng-model="signupCtrl.user.username"
required>

<input class="input-std"
type="text"
placeholder="email"
ng-model="signupCtrl.user.email">

<input class="input-std"
type="text"
placeholder="password"
ng-minlength="3"
ng-model="signupCtrl.user.password"
required>

<input type="submit" value="sign up">
</form>
</section>
21 changes: 21 additions & 0 deletions lab-teddy/app/component/landing/signup/signup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

require('./_signup.scss');

module.exports = {
template: require('./signup.html'),
controller: ['$log', '$location', 'authService', SignupController],
controllerAs: 'signupCtrl',
};

function SignupController($log, $location, authService) {
$log.debug('SignupController');

authService.getToken().then(() => $location.url('/home'));

this.signup = function(user) {
$log.debug('signupCtrl.signup()');

authService.signup(user).then(() => $location.url('/home'));
};
}
7 changes: 7 additions & 0 deletions lab-teddy/app/config/log-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = ['$logProvider', logConfig];

function logConfig($logProvider) {
$logProvider.debugEnabled(__DEBUG__);
}
29 changes: 29 additions & 0 deletions lab-teddy/app/config/router-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = ['$stateProvider', '$urlRouterProvider', routerConfig];

function routerConfig($stateProvider, $urlRouterProvider) {
$urlRouterProvider.when('', '/home');
$urlRouterProvider.when('/', '/join#signup');
$urlRouterProvider.when('/signup', '/join#signup');
$urlRouterProvider.when('/login', '/join#login');

let routes = [
{
name: 'home',
url: '/home',
template: require('../view/home/home.html'),
controller: 'HomeController',
controllerAs: 'homeCtrl',
},
{
name: 'landing',
url: '/join',
template: require('../view/landing/landing.html'),
controller: 'LandingController',
controllerAs: 'landingCtrl',
},
];

routes.forEach(route => $stateProvider.state(route));
}
26 changes: 26 additions & 0 deletions lab-teddy/app/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

require('./scss/main.scss');

const path = require('path');
const camelcase = require('camelcase');
const pascalcase = require('pascalcase');
const angular = require('angular');
const uiRouter = require('@uirouter/angularjs');

const cfgram = angular.module('cfgram', ['ui.router']);

let context = require.context('./config/', true, /\.js$/);
context.keys().forEach( path => cfgram.config(context(path)));

context = require.context('./view/', true, /\.js$/);
context.keys().forEach( key => cfgram.controller(pascalcase(path.basename(key, '.js')), context(key)));

context = require.context('./service/', true, /\.js$/);
context.keys().forEach( key => cfgram.service(camelcase(path.basename(key, '.js')), context(key)));

context = require.context('./component/', true, /\.js$/);
context.keys().forEach( key => {
cfgram.component(camelcase(path.basename(key, '.js')), context(key));
console.log('this is keys', key);
});
17 changes: 17 additions & 0 deletions lab-teddy/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en" ng-app="cfgram">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>routes app</title>
</head>
<body>
<header>
<h1>routes app</h1>
</header>
<main>
<ui-view></ui-view>
</main>
<footer></footer>
</body>
</html>
Empty file added lab-teddy/app/scss/main.scss
Empty file.
90 changes: 90 additions & 0 deletions lab-teddy/app/service/auth-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

module.exports = [
'$q',
'$log',
'$http',
'$window',
function($q, $log, $http, $window, authService) {
$log.debug('authService');

let service = {};
let token = null;

function setToken(_token) {
$log.debug('authService.setToken()');

if(!_token) return $q.reject(new Error('No token'));
$window.localStorage.setItem('token', _token);
token = _token;

return $q.resolve(token);
}

service.getToken = function() {
$log.debug('authService.getToken()');

if(token) return $q.resolve(token);
token = $window.localStorage.getItem('token');
if(token) return $q.resolve(token);

return $q.reject(new Error('Token not found'));
};

service.logout = function() {
$log.debug('authService.logout()');

$window.localStorage.removeItem('token');
token = null;

return $q.resolve();
};

service.signup = function(user) {
$log.debug('authService.signup()');

let url = `${__API_URL__}/api/signup`;
let config = {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
};

return $http.post(url, user, config)
.then(res => {
$log.log('success', res.data);
return setToken(res.data);
})
.catch(err => {
$log.error('failure', err);
return $q.reject(err);
});
};

service.login = function(user) {
$log.debug('authService.login()');

let url = `${__API_URL__}/api/login`;
let base64 = $window.btoa(`${user.username}:${user.password}`);
let config = {
headers: {
Accept: 'application/json',
Authorization: `Basic ${base64}`,
},
};

return $http.get(url, config)
.then(res => {
$log.log('success', res.data);
return setToken(res.data);
})
.catch(err => {
$log.error('failure', err.message);
return $q.reject(err);
});
};

return service;
},
];
Empty file.
9 changes: 9 additions & 0 deletions lab-teddy/app/view/home/home-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('./_home.scss');

module.exports = ['$log', HomeController];

function HomeController($log) {
$log.debug('HomeController()');
}
3 changes: 3 additions & 0 deletions lab-teddy/app/view/home/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<section class="home-content">
<h2>Welcome home!</h2>
</section>
Empty file.
10 changes: 10 additions & 0 deletions lab-teddy/app/view/landing/landing-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

require('./_landing.scss');

module.exports = ['$log', '$location', '$rootScope', 'authService', LandingController];

function LandingController($log, $location, authService) {
let url = $location.url();
this.showSignup = url === '/join#signup' || url === '/join';
}
25 changes: 25 additions & 0 deletions lab-teddy/app/view/landing/landing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<section class="landing">
<div class="join-container"
ng-if="landingCtrl.showSignup">
<div class="join-inner">
<signup></signup>
<p>already a member?</p>
<a href="/#!/login"
ng-click="landingCtrl.showSignup=false">
sign in here.
</a>
</div>
</div>

<div class="join-container"
ng-if="!landingCtrl.showSignup">
<div class="join-inner">
<login></login>
<p>want to signup?</p>
<a href="/#!/signup"
ng-click="landingCtrl.showSignup=true">
sign up here.
</a>
</div>
</div>
</section>
Loading