How to use UserInitializer method in argos

Best JavaScript code snippet using argos

user.js

Source:user.js Github

copy

Full Screen

1/**2 * This module handles the currently logged in user.3 */4(function(ng, $) {5 var module = ng.module("user", [6 "router",7 "resources"8 ]);9 // define url /register and /login10 module.config(function(routerProvider) {11 routerProvider.when("login", "/login", {12 controller: "loginCtrl",13 templateUrl: "/html/login.html"14 });15 routerProvider.when("register", "/register", {16 controller: "registerCtrl",17 templateUrl: "/html/register.html"18 });19 routerProvider.when("logout", "/logout", {20 controller: "logoutCtrl",21 templateUrl: "/html/login.html"22 });23 });24 /**25 * This provider returns a function which can be used to get the current logged in user.26 * The function returns a promise which will be resolved when the user was loaded.27 * If rejectIfNotLoggedIn was set to true then the promise will be rejected if no user28 * is logged in and the user will redirected to the login page.29 *30 * If the user is already in the scope then the user in the scope will be used.31 *32 * The user can be used to block controllers which requires the user.33 */34 module.provider("getUser", function() {35 var userChecked = false;36 this.$get = function($rootScope, loggedInUserResource, userInitializer, $q, router) {37 return function(rejectIfNotLoggedIn) {38 return $q(function(resolve, reject) {39 // handles the promise when no user is logged in.40 var handleNotLoggedIn = function() {41 if(rejectIfNotLoggedIn) {42 // reject and redirect to login43 reject("User is not logged in.");44 router.go("login");45 }46 else {47 resolve(null);48 }49 };50 if(userChecked) {51 // if the user was already checked52 if($rootScope.user) {53 resolve($rootScope.user);54 }55 else {56 handleNotLoggedIn();57 }58 return;59 }60 // load the logged in user from the server.61 loggedInUserResource.get().$promise.then(function(data) {62 userInitializer(data);63 resolve($rootScope.user);64 userChecked = true;65 }, function() {66 userInitializer(null);67 handleNotLoggedIn();68 userChecked = true;69 });70 });71 };72 };73 });74 /**75 * Puts the given user in the scope and sets functions on it to fly with, fly away and check if it is flying with a user.76 * Every change (logouts too) has to be made with this service.77 */78 module.service("userInitializer", function($rootScope, $injector) {79 return function(user) {80 if(user) {81 if($rootScope.user) {82 // new user data -> extend with old user83 ng.extend($rootScope.user, user);84 }85 else {86 // login -> add to scope.87 $rootScope.user = user;88 }89 }90 else {91 // logout -> remove from scope.92 delete $rootScope.user;93 }94 if($rootScope.user) {95 // remove properties which aren't in the model96 delete $rootScope.user.$promise;97 delete $rootScope.user.$resolved;98 // load flyWithResource asynchronously to avoid circular dependencies99 var flyWithResource = $injector.get("flyWithResource");100 /**101 * The logged in user starts flying with the given user.102 * @param flyWithId number - the user to fly with.103 * @param callback function - which will be called after the API call was successful.104 */105 $rootScope.user.flyWith = function(flyWithId, callback) {106 flyWithResource.post({107 userId: $rootScope.user.id,108 flyWithId: flyWithId109 }).$promise.then(function(data) {110 // update model111 $rootScope.user.flyWiths.push(data);112 // call callback if existing113 if(ng.isFunction(callback)) {114 callback(data);115 }116 });117 };118 /**119 * The logged in user stops flying with the given user.120 * @param flyWithId number - the user to fly away.121 * @param callback function - which will be called after the API call was successful.122 */123 $rootScope.user.flyAway = function(flyWithId, callback) {124 flyWithResource.delete({125 userId: $rootScope.user.id,126 flyWithId: flyWithId127 }).$promise.then(function() {128 // update model129 for (var i = 0; i < $rootScope.user.flyWiths.length; i ++) {130 if ($rootScope.user.flyWiths[i].id === flyWithId) {131 $rootScope.user.flyWiths.splice(i, 1);132 break;133 }134 }135 // call callback if existing136 if(ng.isFunction(callback)) {137 callback();138 }139 });140 };141 /**142 * Checks if the logged in user is flying with the given user143 * @param flyWithId number - the user to check.144 * @return boolean - true if the logged in user is flying with the given user.145 */146 $rootScope.user.isFlyingWith = function(flyWithId) {147 for (var i = 0; i < $rootScope.user.flyWiths.length; i ++) {148 if ($rootScope.user.flyWiths[i].id === flyWithId) {149 return true;150 }151 }152 return false;153 };154 }155 };156 });157 /**158 * This controller provides the model for the login data and a login function which submits the data to the server.159 * If the login was successful then it will be initialized with the userInitializer and redirected to the home view160 * or the view which was set in the query params. If the login was unsuccessful then an error message will be added161 * to the scope.162 */163 module.controller("loginCtrl", function(loggedInUserResource, router, $location, $translate, pageTitle, userInitializer, $scope) {164 // change page title165 $translate("pageTitles.login").then(function(title) {166 pageTitle.change(title);167 });168 // model for login data169 $scope.data = {170 username: "",171 password: ""172 };173 // clear all error when the login data changes.174 $scope.$watchCollection("data", function() {175 $scope.errors = [];176 });177 /**178 * Submits the login model to the server.179 */180 $scope.login = function() {181 $scope.errors = [];182 // submit the login data.183 loggedInUserResource.post($scope.data).$promise.then(function(data) {184 // initialize the user model.185 userInitializer(data.model);186 // check if there is return to url param and redirect to it if it exists.187 var queryParams = $location.search();188 if(queryParams.returnTo) {189 $location.url(queryParams.returnTo);190 }191 else {192 // redirect to home view193 router.go("home");194 }195 }, function(data) {196 if(data.status == 401) {197 // put error message into the scope.198 $translate("errors.login.wrongCredentials").then(function(error) {199 $scope.errors.push(error);200 });201 }202 });203 };204 });205 /**206 * Provides a model for the register data. The provided register function can be used to submit this data to the server.207 * If the user was successfully registered then it will be put into the scope with the userInitializer and redirected208 * to the home view. If the registration was unsuccessful then the errors will be added to the scope.209 */210 module.controller("registerCtrl", function(usersResource, router, $translate, pageTitle, localeUtil, userInitializer, $scope) {211 // change page title.212 $translate("pageTitles.register").then(function(title) {213 pageTitle.change(title);214 });215 // initialize model for registration data216 $scope.data = {217 email: "",218 username: "",219 displayName: "",220 password: "",221 passwordRetype: "", // will not be sent to the server222 locale: localeUtil.getCurrent()223 };224 /**225 * Registers the user with a server call.226 */227 $scope.register = function() {228 var userData = ng.copy($scope.data);229 // not in the model of the server -> remove it.230 delete userData.passwordRetype;231 // call REST resource.232 usersResource.post(userData).$promise.then(function(data) {233 userInitializer(data.model);234 delete $scope.errors; // remove old errors235 // after registration -> go to home.236 router.go("home");237 },238 function(response) {239 // add error messages to the scope.240 $scope.success = response.data.success;241 delete $scope.successMsg;242 $scope.errors = response.data;243 });244 };245 });246 /**247 * This controller does a logout on the server and with the userInitializer and redirects248 * the user to the login view.249 */250 module.controller("logoutCtrl", function(loggedInUserResource, router, userInitializer) {251 // call REST resource.252 loggedInUserResource.delete().$promise.then(function() {253 userInitializer(null); // remove254 // after logout -> go to login.255 router.go("login");256 });257 });258 // register authentication check interceptor259 module.config(function($httpProvider) {260 $httpProvider.interceptors.push("authCheckInterceptor");261 });262 /**263 * Checks every response error if the response status code is 401. In this case the264 * user will be redirected to the login view.265 * The 401 code means that no user is logged in but one is required.266 */267 module.service("authCheckInterceptor", function($q, $log, $location, userInitializer) {268 return {269 responseError: function(rejection) {270 // check if response code is 401 and we're not already on the login view.271 if (rejection.status === 401 && $location.path() != "/login") {272 $log.info("Response code was 401.", rejection);273 userInitializer(null); // logout user.274 // we can't use our router because of problems with DI.275 $location.path("/login").search("returnTo", $location.path());276 }277 return $q.reject(rejection);278 }279 };280 });...

Full Screen

Full Screen

routes.ts

Source:routes.ts Github

copy

Full Screen

...22 host: 'localhost',23 database: 'webshop',24 dialect: 'mysql'25 });26 const userInitializer = new UserInitializer();27 const fileInitializer = new FileInitializer();28 const itemInitializer = new ItemInitializer();29 userInitializer.init(this.connection);30 fileInitializer.init(this.connection);31 itemInitializer.init(this.connection);32 const userController: UserController = new UserController();33 const fileController: FileController = new FileController();34 const itemController: ItemController = new ItemController();35 this.myRoutes = [36 new MainRouter(app, Express.Router(), userController),37 new FilesRoute(app, Express.Router(), fileController),38 new ItemsRoute(app, Express.Router(), itemController),39 new UsersRoute(app, Express.Router(), userController)40 ];...

Full Screen

Full Screen

userInitializer.ts

Source:userInitializer.ts Github

copy

Full Screen

...37// };38// }39// let userInitializer: UserInitializer = null;40// if (!userInitializer) {41// userInitializer = new UserInitializer();42// }43// export default userInitializer...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var UserInitializer = require('argos-sdk/src/Models/UserInitializer');2UserInitializer.initialize();3var UserInitializer = require('argos-sdk/src/Models/UserInitializer');4UserInitializer.initialize();5var UserInitializer = require('argos-sdk/src/Models/UserInitializer');6UserInitializer.initialize();7var UserInitializer = require('argos-sdk/src/Models/UserInitializer');8UserInitializer.initialize();9var UserInitializer = require('argos-sdk/src/Models/UserInitializer');10UserInitializer.initialize();11var UserInitializer = require('argos-sdk/src/Models/UserInitializer');12UserInitializer.initialize();13var UserInitializer = require('argos-sdk/src/Models/UserInitializer');14UserInitializer.initialize();15var UserInitializer = require('argos-sdk/src/Models/UserInitializer');16UserInitializer.initialize();17var UserInitializer = require('argos-sdk/src/Models/UserInitializer');18UserInitializer.initialize();19var UserInitializer = require('argos-sdk/src/Models/UserInitializer');20UserInitializer.initialize();21var UserInitializer = require('argos-sdk/src/Models/UserInitializer');22UserInitializer.initialize();23var UserInitializer = require('argos-sdk/src/Models/UserInitializer');24UserInitializer.initialize();25var UserInitializer = require('argos-sdk/src/Models/UserInitializer');26UserInitializer.initialize();27var UserInitializer = require('argos-sdk/src/Models/UserInitializer');28UserInitializer.initialize();29var UserInitializer = require('argos-sdk/src/Models/UserInitializer');30UserInitializer.initialize();

Full Screen

Using AI Code Generation

copy

Full Screen

1var UserInitializer = require('argos/UserInitializer');2UserInitializer.initialize();3var UserInitializer = require('argos-sdk/UserInitializer');4UserInitializer.initialize();5var UserInitializer = require('argos/UserInitializer');6var user = UserInitializer.initialize();7var UserInitializer = require('argos-sdk/UserInitializer');8var user = UserInitializer.initialize();9var UserInitializer = require('argos/UserInitializer');10var user = UserInitializer.initialize({ test: 'test' });11var UserInitializer = require('argos-sdk/UserInitializer');12var user = UserInitializer.initialize({ test: 'test' });

Full Screen

Using AI Code Generation

copy

Full Screen

1var argosSdk = require('argos-sdk');2var UserInitializer = argosSdk.UserInitializer;3var userInitializer = new UserInitializer();4userInitializer.initializeUser();5#### `initializeUser()`6#### `isUserInitialized()`7#### `initializeUser()`8#### `isUserInitialized()`9#### `initializeUser()`10#### `isUserInitialized()`11#### `initializeUser()`12#### `isUserInitialized()`

Full Screen

Using AI Code Generation

copy

Full Screen

1var UserInitializer = require('argos-sdk/src/UserInitializer');2var userInitializer = new UserInitializer();3userInitializer.initialize();4| initialize | (callback) | Initializes the user. If the user is authenticated, it will call the callback function. If the user is not authenticated, it will prompt for authentication. | 5| authenticate | (callback) | Prompts for authentication. If the user is authenticated, it will call the callback function. | 6| register | (callback) | Prompts for registration. If the user is registered, it will call the callback function. | 7| isUserDisabled | (callback) | Returns true if the user is disabled, otherwise false. | 8| isUserExpired | (callback) | Returns true if the user is expired, otherwise false. | 9| isUserLockedOut | (callback) | Returns true if the user is locked out, otherwise false. | 10var UserInitializer = require('argos-sdk/src/UserInitializer');11var userInitializer = new UserInitializer();12userInitializer.initialize(function() {13 console.log('User is authenticated');14});15* [App](./App)16* [Environment](./Environment)17* [User](./User)18* [UserManager](./UserManager)19* [UserModule](

Full Screen

Using AI Code Generation

copy

Full Screen

1const path = require('path');2const { UserInitializer } = require('argos-sdk');3const userInitializer = new UserInitializer({4 userCodePath: path.join(__dirname, 'user-code'),5 userDataPath: path.join(__dirname, 'user-data'),6 userConfigPath: path.join(__dirname, 'user-config'),7 userLogPath: path.join(__dirname, 'user-logs'),8 userTempPath: path.join(__dirname, 'user-temp'),9 userCachePath: path.join(__dirname, 'user-cache'),10});11const environmentVariables = {12};13userInitializer.initialize({14 userCodePath: path.join(userInitializer.userCodePath, 'index.js'),15});

Full Screen

Using AI Code Generation

copy

Full Screen

1var argos = require('argos-sdk');2var initializer = argos.UserInitializer;3initializer.initializeUser();4var argos = require('argos-sdk');5var analytics = argos.Analytics;6analytics.logPageView('pageName');7var argos = require('argos-sdk');8var analytics = argos.Analytics;9analytics.logEvent('eventName');10var argos = require('argos-sdk');11var analytics = argos.Analytics;12analytics.logError('errorName');13var argos = require('argos-sdk');14var analytics = argos.Analytics;15analytics.logException('exceptionName');16var argos = require('argos-sdk');17var analytics = argos.Analytics;18analytics.logCustomEvent('eventName', {customData: 'customData'});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run argos automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful