Skip to content

Instantly share code, notes, and snippets.

@vishwassharma
Forked from clouddueling/MainCtrl.js
Created November 1, 2013 00:17
Show Gist options
  • Save vishwassharma/7259294 to your computer and use it in GitHub Desktop.
Save vishwassharma/7259294 to your computer and use it in GitHub Desktop.

Revisions

  1. @clouddueling clouddueling revised this gist Aug 9, 2013. 1 changed file with 46 additions and 1 deletion.
    47 changes: 46 additions & 1 deletion MainCtrl.js
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,51 @@ controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootSc
    });
    }

    $scope.loadAuth();

    $scope.logoutUser = function() {
    Auth.logout().success(function(data) {
    toastr.info("You have been logged out.");
    $scope.main.user = {};
    });
    }

    $scope.loginUser = function() {
    Auth.login({
    username: $scope.main.credentials.email,
    password: $scope.main.credentials.password
    }).success(function(data) {
    if (data.error) {
    toastr.error(data.error);
    } else {
    toastr.success("You are signed in!");
    $scope.loadAuth();
    $scope.main.credentials = {};
    Popup.close();
    }
    });
    }

    $scope.registerUser = function() {
    Auth.register({
    serie_id: $scope.main.serieId,
    email: $scope.newUser.email,
    password: $scope.newUser.password,
    terms: $scope.newUser.terms,
    name: $scope.newUser.name,
    }).success(function(data) {
    if (data.error) {
    toastr.error(data.error);
    }

    if (data.success) {
    toastr.success("Welcome to " + $scope.main.serie.name + "!");
    $scope.loadAuth();
    $scope.newUser = {};
    Popup.close();
    }
    });
    }

    $scope.loadAuth();
    $scope.loadSerie();
    });
  2. @clouddueling clouddueling created this gist Aug 9, 2013.
    17 changes: 17 additions & 0 deletions MainCtrl.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    controllers.controller('MainCtrl', function($scope, $location, Facebook, $rootScope, $http, $location, Upload, Auth, User, Question, Category, Serie, Record, Location, Popup, Process, Card, Question) {
    $scope.$on('authLoaded', function() {
    $scope.isExpert($scope.main.serieId);
    $scope.isMember($scope.main.serieId);
    });

    $scope.loadAuth = function() {
    Auth.load().success(function(data) {
    $scope.main.user = data.user;
    $scope.$broadcast("authLoaded");
    Popup.close();
    });
    }

    $scope.loadAuth();
    $scope.loadSerie();
    });
    22 changes: 22 additions & 0 deletions auth.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    services.factory('Auth', function($http){
    return {
    load: function() {
    return $http.get('/api/v1/auth');
    },
    logout: function() {
    return $http.get('/api/v1/auth/logout');
    },
    login: function(inputs) {
    return $http.post('/api/v1/auth/login', inputs);
    },
    register: function(inputs) {
    return $http.post('/api/v1/auth/register', inputs);
    },
    locations: function() {
    return $http.get('/api/v1/auth/locations');
    },
    check: function() {
    return $http.get('/api/v1/auth/check');
    }
    }
    });
    152 changes: 152 additions & 0 deletions auth.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    <?php

    class Api_V1_Auth_Controller extends Base_Controller
    {
    public function get_index()
    {
    try {
    $user = array();
    if (Auth::check()) {
    $user = Auth::user();
    $user->image = read_image($user->image);
    }

    if ($user)
    $user->password = null;

    $data = array(
    'user' => to_json($user),
    );

    return json($data);
    } catch(Exception $e) {
    return $e->getMessage();
    }
    }

    public function get_logout()
    {
    try {
    return Auth::logout();
    } catch(Exception $e) {
    return $e->getMessage();
    }
    }

    public function post_login()
    {
    try {
    $s = extend(array(
    'username' => '',
    'password' => '',
    ), sr());

    if (! Auth::attempt($s))
    throw new Exception("Incorrect email or password.");

    if (Auth::user()->account_user_id == 0 || Auth::user()->deleted)
    throw new Exception("We can't find the account you are associated with.");

    $data = array(
    'success' => ''
    );

    return json($data);
    } catch(Exception $e) {
    Auth::logout();

    $data = array(
    'error' => $e->getMessage()
    );

    return json($data);
    }
    }

    public function post_register()
    {
    try {
    $s = extend(array(
    'serie_id' => 0,
    'name' => '',
    'email' => '',
    'password' => '',
    'terms' => '0',
    ), sr());

    $v = Validator::make(array(
    'email' => $s['email'],
    'password' => $s['password'],
    'terms' => $s['terms'],
    ), array(
    'email' => 'email|required|unique:users',
    'password' => 'required|min:6',
    'terms' => 'accepted|required',
    ));

    if ($v->fails()) {
    foreach ($v->errors->messages as $key => $value) {
    $error = $value[0];
    break;
    }

    throw new Exception($error);
    }

    $user = User::create(array(
    'name' => $s['name'],
    'email' => $s['email'],
    'password' => Hash::make($s['password']),
    'type' => 'account',
    'perm_contacts' => 0,
    ));

    $user->account_user_id = $user->id;
    $user->save();
    Auth::login($user->id, 1);

    $serie = Serie::find($s['serie_id']);
    if (! is_null($serie))
    if (! $serie->is_member($user->id) && ! $serie->is_expert($user->id))
    $serie->create_member($user->id);

    $data = array(
    'success' => true,
    );

    return json($data);
    } catch(Exception $e) {
    $data = array(
    'error' => $e->getMessage()
    );

    return json($data);
    }
    }

    public function get_locations()
    {
    try {
    $data = array(
    'locations' => to_json(Auth::user()->locations),
    );

    return json($data);
    } catch(Exception $e) {
    Report::log($e->getMessage());
    }
    }

    public function get_check()
    {
    try {
    $data = array(
    'check' => Auth::check(),
    );

    return json($data);
    } catch(Exception $e) {
    Report::log($e->getMessage());
    }
    }
    }