I'm beginner in Ionic. I just try to implement account kit authentication with ionic, but I always got this error
Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:nomod] Module 'starter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
and how is the best practice to implement account kit with Ionic framework?
And this is my app.js
angular.module('starter', ['ionic', 'starter.services', 'firebase', 'AccountKit'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
StatusBar.styleDefault();
}
})
})
// Start of Controller
.controller('LoginCtrl', function($scope){
// initialize Account Kit with CSRF protection
$scope.AccountKit_OnInteractive = function(response){
AccountKit.init({
appId:'secret',
state:"secret",
version:"v1.1"
})
}
})
.controller('DashCtrl', function($scope) {})
.controller('ChatsCtrl', function($scope, Chats) {
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
};
})
.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
$scope.chat = Chats.get($stateParams.chatId);
})
.controller('AccountCtrl', function($scope) {
$scope.settings = {
enableFriends: true
};
});
// End of Controller
// Start Routing
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginCtrl'
})
// setup an abstract state for the tabs directive
.state('tab', {
url: '/tab',
abstract: true,
templateUrl: 'templates/tabs.html'
})
// Each tab has its own nav history stack:
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
}
})
.state('tab.chats', {
url: '/chats',
views: {
'tab-chats': {
templateUrl: 'templates/tab-chats.html',
controller: 'ChatsCtrl'
}
}
})
.state('tab.chat-detail', {
url: '/chats/:chatId',
views: {
'tab-chats': {
templateUrl: 'templates/chat-detail.html',
controller: 'ChatDetailCtrl'
}
}
})
.state('tab.account', {
url: '/account',
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html',
controller: 'AccountCtrl'
}
}
})
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/login');
});
// End of Routing
// Initialize Firebase
var config = {
apiKey: "secret",
authDomain: "secret",
databaseURL: "secret",
storageBucket: "secret",
messagingSenderId: "secret"
};
firebase.initializeApp(config);
Related
Working on an Ionic version 1.3.3 application where need following functionalities for user login. I had go through all stackoverflow answer but nothing found a workable solution for me.
App will check on start if user already logged in (check through Ionic $localstorage) then redirect to Home page
If the user is not logged redirect to login page on app start
On login page after login success redirect to home page and clear login page history.
angular.module('starter', ['ionic', 'starter.controllers', 'starter.directives', 'starter.services', 'ngStorage','ab-base64',])
.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
cache: false,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl',
onEnter: function ($state) {
console.log($state);
}
})
.state('app.home', {
cache: false,
url: '/home',
views: {
'menuContent': {
templateUrl: 'templates/home.html'
}
}
})
.state('app.login', {
cache: false,
url: '/login/:username/:password',
views: {
'menuContent': {
templateUrl: 'templates/login.html',
controller: 'LoginController'
}
}
})
.state('app.profile', {
cache: false,
url: '/profile',
views: {
'menuContent': {
templateUrl: 'templates/profile.html',
controller: 'ProfileController'
}
}
})
$urlRouterProvider.otherwise('/app/home');
})
This is how I accomplished this in Ionic v1:
For the redirect if user is logged in:
.state("app.dash", {
url: "/dashboard",
abstract: true,
views: {
mainContent: {
templateUrl: "templates/dashboard.html",
controller: "DashboardCtrl",
controllerAs: "vm",
resolve: {
auth: [
"authService",
function(authService) {
return authService.isAuthenticated();
}
],
permissions: [
"authService",
function(authService) {
return authService.getPermissions();
}
]
}
}
}
})
For the redirect when user logs in or is already logged in.
.state("app.login", {
url: "/login?accountCreated",
views: {
mainContent: {
templateUrl: "templates/login.html",
controller: "LoginCtrl",
controllerAs: "vm",
resolve: {
isLoggedIn: [
"$q",
"$state",
"authService",
function($q, $state, authService) {
authService.isAuthenticated().then(function(res) {
$state.go("app.dash.home");
});
return $q.defer().resolve();
}
]
}
}
}
})
Auth service isAuthenticated()
function isAuthenticated() {
var deferred = $q.defer();
getToken().then(function(token) {
isExpired().then(function(isExpired) {
if (!token || isExpired) {
deferred.reject("Not Authenticated");
} else {
decodeToken().then(function(decodedToken) {
deferred.resolve(decodedToken);
});
}
});
});
return deferred.promise;
}
Hello I am working with ionic framework and I want to redirect to another page
after successfully login .When I run project using command:
ionic serve then $state.go working properly but when I run project using
ionic serve --lab it is not working
.controller('AppCtrl', function($scope,$http, $ionicModal,$location,$state, $timeout) {
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
alert($scope.loginData.username);
alert($scope.loginData.password);
$http({
method: 'POST',
url: 'http://localhost/home_owner12/admin/api/login',
data: {username:$scope.loginData.username,password:$scope.loginData.password},
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'}
})
.then(function successCallback(response)
{
if(response.data.length > 0)
{
$state.go('app.search');
console.log('the state is '+$state.current);
}
else
{
alert("Invalid email or pasword");
///$location.path('/search');
}
}, function errorCallback(response)
{
alert("Invalid email pasword");
});
};
})
here is state:
config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html',
controller: 'AppCtrl'
}
}
});
and module
angular.module('starter', ['ionic', 'starter.controllers', 'ui.router'])
The app.search page's controller goes to AppCtrl in place.
Because AppCtrl runs a second time, it is entering an infinite loop.
Replace the code with that
config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.search', {
url: '/search',
views: {
'menuContent': {
templateUrl: 'templates/search.html',
controller: 'SearchCtrl'
}
}
});
and
.controller('SearchCtrl', function($scope,$http, $ionicModal,$location,$state, $timeout) {
............
.........
.......
})
I know this question has been asked several times, but I'm not understanding if I missed anything or it's just a server thing.
I need to show you my code for checking please:
(function(){
var app = angular.module('c4s', ['ionic', 'starter.controllers', 'starter.services']);
app.controller('curiousCtrl', function($http, $scope){
$scope.stories = [];
$http.jsonp('http://curious4science.com/?json=1&callback=JSON_CALLBACK')
.success(function(response) {
angular.forEach(response.data.children, function(child){
console.log(response);
$scope.stories.push(child.data);
});
});
});
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleLightContent();
}
});
})
.config(function($httpProvider, $stateProvider, $urlRouterProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html"
})
// Each tab has its own nav history stack:
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
}
})
.state('tab.chats', {
url: '/chats',
views: {
'tab-chats': {
templateUrl: 'templates/tab-chats.html',
controller: 'ChatsCtrl'
}
}
})
.state('tab.chat-detail', {
url: '/chats/:chatId',
views: {
'tab-chats': {
templateUrl: 'templates/chat-detail.html',
controller: 'ChatDetailCtrl'
}
}
})
.state('tab.account', {
url: '/account',
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html',
controller: 'AccountCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/dash');
});
}());
And I'm getting this error:
TypeError: response.data is undefined
First, I tried $http.get(....) but I got the error that cross domain thing is not allowed. Then I tried jsop.
Thank you.
The sucess() callback doesn't take the http response as argument. It takes the response data, then the status, then the headers, then the config.
So the code should be
$http.jsonp('http://curious4science.com/?json=1&callback=JSON_CALLBACK')
.success(function(data) {
angular.forEach(data.children, function(child){
console.log(response);
$scope.stories.push(child.data);
});
});
(assuming the returned JSON has a children field which is an array, and that each child has a data field, of course).
I'm new on Ionic, and I have been a couple of days with this problem.
I create an app from chat examples, and I want to connect and read to my firebase database. The first part is woking.- I can retreive and show data from firebase, but my problem is when I click on the "Item list" and want to show detail description, I don not understand how to pass value and get the data again.
Here are my scripst:
app.js ( I'm only showing part of them )
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','firebase'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleLightContent();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html"
})
// Each tab has its own nav history stack:
.state('tab.dash', {
url: '/dash',
views: {
'tab-dash': {
templateUrl: 'templates/tab-dash.html',
controller: 'DashCtrl'
}
}
})
.state('tab.chats', {
url: '/chats',
views: {
'tab-chats': {
templateUrl: 'templates/tab-chats.html',
controller: 'ChatsCtrl'
}
}
})
.state('tab.chat-detail', {
url: '/chats/:chatId',
views: {
'tab-chats': {
templateUrl: 'templates/chat-detail.html',
controller: 'ChatDetailCtrl'
}
}
})
.state('tab.account', {
url: '/account',
views: {
'tab-account': {
templateUrl: 'templates/tab-account.html',
controller: 'AccountCtrl'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/tab/dash');
});
Here is my services file --- services.js where I connect to Firebase
angular.module('starter.services', [])
.factory('fireBaseData', function($firebase) {
// Might use a resource here that returns a JSON array
var ref = new Firebase("https://scorching-fire-921.firebaseio.com/")
refCorales = new Firebase("https://scorching-fire- 921.firebaseio.com/corales/");
var fireBaseData = {
all: refCorales,
get: function (chatId) {
return $firebase(ref.child('refCorales').child(chatId)).$asObject();
}
};
return {
ref: function() {
return ref;
},
refCorales: function() {
return refCorales;
}
}
});
And finally here is my controller.js
angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope) {})
.controller('ChatsCtrl', function($scope, $firebase, fireBaseData) {
$scope.corales = $firebase(refCorales);
})
.controller('ChatDetailCtrl', function($scope, $stateParams, fireBaseData) {
$scope.corales = refCorales.get($stateParams.chat$id);
})
.controller('AccountCtrl', function($scope) {
$scope.settings = {
enableFriends: true
};
});
When I click on any item of the list, I'm receiving the following error message: TypeError: refCorales.get is not a function
Any idea how to avoid this erro?
In advance, thank you !
Victor
The problem i see is your services code is very unclear and not returning a way to acces var fireBaseData. Also why are you making two firebase references and not simple using ref.child('corales')?
This would be my solution:
.factory('fireBaseData', function($firebase) {
//Firebase reference
var ref = new Firebase("https://scorching-fire-921.firebaseio.com/")
return {
ref: function(){
return ref;
},
//I don't know if this is actually necessary
refCorales: function(){
return ref.child('corales');
},
get: function(chatId){
$firebase(ref.child('corales').child(chatId)).$asObject();
}
};
})
Update
Also some changes for your controller:
.controller('ChatsCtrl', function($scope, fireBaseData) {
$scope.corales = fireBaseData.refCorales();
})
.controller('ChatDetailCtrl', function($scope, $stateParams, fireBaseData) {
$scope.corales = fireBaseData.get($stateParams.chat$id);
})
I am creating a mobile app in angularjs . I call a resource that calls an API to give me values. Everything works fine , but with slow connections or 3G $ scope not cool me and therefore when browsing the list of items is old
SERVICES.JS
.factory('Exercises', function($resource) {
// localhost: Local
// 79.148.230.240: server
return $resource('http://79.148.230.240:3000/wodapp/users/:idUser/exercises/:idExercise', {
idUser: '55357c898aa778b657adafb4',
idExercise: '#_id'
}, {
update: {
method: 'PUT'
}
});
});
CONTROLLERS
.controller('ExerciseController', function($q, $scope, $state, Exercises) {
// reload exercises every time when we enter in the controller
Exercises.query(function(data) {
$scope.exercises = data;
});
// refresh the list of exercises
$scope.doRefresh = function() {
// reload exercises
Exercises.query().$promise.then(function(data) {
$scope.exercises = data;
}, function(error) {
console.log('error');
});
// control refresh element
$scope.$broadcast('scroll.refreshComplete');
$scope.$apply();
}
// create a new execersie template
$scope.newExercise = function() {
$state.go('newExercise');
};
// delete a exercise
$scope.deleteExercise = function(i) {
// we access to the element using index param
var exerciseDelete = $scope.exercises[i];
// delete exercise calling Rest API and later remove to the scope
exerciseDelete.$delete(function() {
$scope.exercises.splice(i, 1);
});
};
})
APP.js
angular.module('wodapp', ['ionic', 'ngResource', 'wodapp.controllers','wodapp.services'])
// Run
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// ionic is loaded
});
})
// Config
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$stateProvider
.state('slide', {
url: '/',
templateUrl: 'templates/slides.html',
controller: 'SlideController'
})
.state('login', {
url: '/login',
templateUrl: 'templates/login.html',
controller: 'LoginController'
})
.state('dashboard', {
url: '/dashboard',
templateUrl: 'templates/dashboard.html',
controller: 'DashboardController'
})
.state('exercise', {
url: '/exercise',
templateUrl: 'templates/exercises.html',
controller: 'ExerciseController'
})
.state('newExercise',{
url: '/newExercise',
templateUrl: 'templates/newExercise.html',
controller: 'NewExerciseController'
});
$urlRouterProvider.otherwise('/');
});