mirror of
https://github.com/jcwimer/wrestlingApp
synced 2026-04-04 22:03:49 +00:00
Frontend authentication working.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
var app = angular.module("wrestlingdev", ["ngRoute"]).run(function($rootScope) {
|
||||
var app = angular.module("wrestlingdev", ["ngRoute","Devise"]).run(function($rootScope) {
|
||||
// adds some basic utilities to the $rootScope for debugging purposes
|
||||
$rootScope.log = function(thing) {
|
||||
console.log(thing);
|
||||
|
||||
51
frontend/app/js/controllers/login-controller.js
Normal file
51
frontend/app/js/controllers/login-controller.js
Normal file
@@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
app.controller("loginController", function($scope, $routeParams, Auth, $rootScope) {
|
||||
$scope.credentials = {
|
||||
email: '',
|
||||
password: ''
|
||||
};
|
||||
|
||||
var config = {
|
||||
headers: {
|
||||
'X-HTTP-Method-Override': 'POST'
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
$scope.login = function(){
|
||||
Auth.login($scope.credentials, config).then(function(user) {
|
||||
console.log(user); // => {id: 1, ect: '...'}
|
||||
$rootScope.user = user;
|
||||
$rootScope.alertClass = "alert alert-success";
|
||||
$rootScope.alertMessage = "Logged in successfully";
|
||||
}, function(error) {
|
||||
console.log(error);
|
||||
$rootScope.alertClass = "alert alert-danger";
|
||||
$rootScope.alertMessage = "Username and/or password is incorrect";
|
||||
});
|
||||
};
|
||||
|
||||
$scope.logout = function(){
|
||||
Auth.logout(config).then(function(oldUser) {
|
||||
// alert(oldUser.name + "you're signed out now.");
|
||||
$rootScope.user = null;
|
||||
$rootScope.alertClass = "alert alert-success";
|
||||
$rootScope.alertMessage = "Logged out successfully";
|
||||
}, function(error) {
|
||||
// An error occurred logging out.
|
||||
$rootScope.alertClass = "alert alert-danger";
|
||||
$rootScope.alertMessage = "There was an error logging out";
|
||||
});
|
||||
};
|
||||
|
||||
Auth.currentUser().then(function(user) {
|
||||
// User was logged in, or Devise returned
|
||||
// previously authenticated session.
|
||||
$rootScope.user = user;
|
||||
}, function(error) {
|
||||
// unauthenticated error
|
||||
$rootScope.user = null;
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
app.controller("tournamentController", function($scope, tournamentsService, $routeParams, Wrestler) {
|
||||
app.controller("tournamentController", function($scope, tournamentsService, $routeParams, Wrestler, Auth, $rootScope) {
|
||||
$scope.message = "Test message in scope.";
|
||||
|
||||
|
||||
// $scope.tournamentData = "test";
|
||||
tournamentsService.tournamentDetails($routeParams.id).then(function(data) {
|
||||
//this will execute when the
|
||||
@@ -9,7 +10,43 @@ app.controller("tournamentController", function($scope, tournamentsService, $rou
|
||||
$scope.tournament = data;
|
||||
});
|
||||
|
||||
// refresh tournament data every 10 seconds
|
||||
// setInterval(function(){
|
||||
// tournamentsService.tournamentDetails($routeParams.id).then(function(data) {
|
||||
// //this will execute when the
|
||||
// //AJAX call completes.
|
||||
// $scope.tournament = data;
|
||||
// });
|
||||
// }, 10000);
|
||||
|
||||
$scope.wrestler = Wrestler;
|
||||
|
||||
$scope.showSchools = false;
|
||||
|
||||
$scope.toggleSchools = function(){
|
||||
$scope.showSchools = !$scope.showSchools;
|
||||
};
|
||||
|
||||
$scope.showWeightSeeds = false;
|
||||
|
||||
$scope.toggleWeightSeeds = function(){
|
||||
$scope.showWeightSeeds = !$scope.showWeightSeeds;
|
||||
};
|
||||
|
||||
$scope.showBoutBoard = false;
|
||||
|
||||
$scope.toggleBoutBoard = function(){
|
||||
$scope.showBoutBoard = !$scope.showBoutBoard;
|
||||
};
|
||||
|
||||
|
||||
$scope.isTournamentOwner = function(tournamentId,userId){
|
||||
if(userId == tournamentId){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
});
|
||||
@@ -4,16 +4,15 @@ app.factory('Wrestler', function Wrestler(){
|
||||
var vm = this;
|
||||
|
||||
|
||||
vm.matches = function(matches,wrestler){
|
||||
var givenWrestler = wrestler;
|
||||
vm.matches = function(wrestler,matches){
|
||||
|
||||
console.log(givenWrestler.id);
|
||||
console.log(matches);
|
||||
return _.filter(matches, function(match){
|
||||
return match.w1 == givenWrestler.id || match.w2 == givenWrestler.id;
|
||||
return match.w1 == wrestler.id || match.w2 == wrestler.id;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
return vm;
|
||||
});
|
||||
@@ -40,5 +40,5 @@ app.config(['$routeProvider', '$locationProvider', function($routeProvider,$loca
|
||||
$routeProvider.otherwise({redirectTo: '/'});
|
||||
|
||||
//this give me normal routes instead of /#/
|
||||
$locationProvider.html5Mode(true);
|
||||
// $locationProvider.html5Mode(true);
|
||||
}]);
|
||||
@@ -1,49 +0,0 @@
|
||||
app.factory('AuthenticationService',
|
||||
['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
|
||||
function (Base64, $http, $cookieStore, $rootScope, $timeout) {
|
||||
var service = {};
|
||||
|
||||
service.Login = function (username, password, callback) {
|
||||
|
||||
/* Dummy authentication for testing, uses $timeout to simulate api call
|
||||
----------------------------------------------*/
|
||||
// $timeout(function(){
|
||||
// var response = { success: username === 'test' && password === 'test' };
|
||||
// if(!response.success) {
|
||||
// response.message = 'Username or password is incorrect';
|
||||
// }
|
||||
// callback(response);
|
||||
// }, 1000);
|
||||
|
||||
|
||||
/* Use this for real authentication
|
||||
----------------------------------------------*/
|
||||
$http.post('/api/authenticate', { username: username, password: password })
|
||||
.success(function (response) {
|
||||
callback(response);
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
service.SetCredentials = function (username, password) {
|
||||
var authdata = Base64.encode(username + ':' + password);
|
||||
|
||||
$rootScope.globals = {
|
||||
currentUser: {
|
||||
username: username,
|
||||
authdata: authdata
|
||||
}
|
||||
};
|
||||
|
||||
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; // jshint ignore:line
|
||||
$cookieStore.put('globals', $rootScope.globals);
|
||||
};
|
||||
|
||||
service.ClearCredentials = function () {
|
||||
$rootScope.globals = {};
|
||||
$cookieStore.remove('globals');
|
||||
$http.defaults.headers.common.Authorization = 'Basic ';
|
||||
};
|
||||
|
||||
return service;
|
||||
}]);
|
||||
@@ -37,8 +37,8 @@ function tournamentsService($http){
|
||||
}
|
||||
|
||||
function errorCallback(err){
|
||||
console.log("error log below");
|
||||
console.log(err);
|
||||
// console.log("error log below");
|
||||
// console.log(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user