Ember not transitioning to profile after logging in - javascript

I am working on an Ember project with a Node backend. I would like to have a user login/ signup on the client side, send an AJAX POST request to the server to check its authentication and then send back a thumbs up or a thumbs down accordingly. If a thumbs up is returned I want to transition the user to their profile. However, when the person submits the form, a successful POST request is sent but nothing happens as far as transitioning to another page. Any ideas as to why this is happening?
Here is what my Route looks like:
// Username for the new user
email: '',
// Password for the new user
password: '',
actions: {
manualLogin: function() {
const email = this.get('email');
const password = this.get('password');
var temp = this;
Ember.$.ajax({
type: "POST",
url: "http://localhost:8080/api/login",
data: { email: email, password: password }
})
.done(function(data) {
console.log('transition to /user, ' + data);
temp.transitionTo('user');
})
.fail(function(data) {
console.log(data);
});
//this.transitionTo('user');
return;
}
}
Here is my Router:
Router.map(function() {
this.route('explore', { path: '/' });
this.route('user');
this.route('login');
this.route('signup');
});
And the code for the submit button with the login template:
<button class="button radius" type="submit" {{action 'manualLogin'}}>Login</button>

Related

How to get detail user after login with laravel passport and show it using jquery

I have some problem with my code, so I have method to login using laravel passport and it's work my token is show up and nothing error in my view, but I wanna ask after I have login, How can I show the detail user? I wanna use for showing the detail user, and also to insert back to database for knowing user activity (like change data, insert data, and deleting data). So first of All my code like this :
In Controller what I wanna call the detail user :
public function index() {
$user = auth()->guard('api')->user();
dd($user) //<= when I look is null value
}
Controller Login :
public function login(Request $login) {
$email = $login->email;
$password = $login->password;
$user = User::where('user.email', $email)->where('user.password', $password)->first();
$success['token'] = $user->createToken('myToken')->accessToken;
return response()->json([
'user' => $user,
'token' => $success
]);
}
Route :
Route::post('login', 'UserController#login');
javascript :
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: "login",
method: "POST",
data: {
email: email,
password: password,
},
}).done(function (data, status) {
localStorage.setItem('myToken', data.token);
$(document).ajaxSend(function (event, jqxhr, settings) {
jqxhr.setRequestHeader('Authorization', "Bearer" + data.token);
});
}).fail(function (error) {
//error in here
})

How to set up session in vuejs django after successful login?

If login is successful i need to redirect to a login success page and I need to include a session in that.. How can it be possible. I am using html with vue js for front end and back end is django.
This is my vue js script for login.
<script>
logBox = new Vue({
el: "#logBox",
data: {
email : '',
password: '',
response: {
authentication : '',
}
},
methods: {
handelSubmit: function(e) {
var vm = this;
data = {};
data['email'] = this.email;
data['password'] = this.password;
$.ajax({
url: '/login/',
data: data,
type: "POST",
dataType: 'json',
success: function(e) {
if(e.status){
alert("Login Success");
// Redirect the user
window.location.href = "/loginsuccess/";
}
else {
vm.response = e;
alert("failed to login!");
}
}
});
return false;
}
},
});
</script>
So, how can I include session in login successpage.. This is the first time I am doing a work.. Please help me ..
If you are not doing a SPA, you can use the usual way of store sessions, put the session token in a cookie (the server is the responsible of doing when a successful login request happens). The next time the user do a request the cookie will send automatically to the server.
If you are doing a SPA, you can store the session token in the localStorage. In this case, you should configure the ajax calls to set a header with the token or any other way you prefer to send the token to the server.

Backbone Login Verification

I'm asking this question in part for a whole class. We are having a hard time getting a token for login. Also, if you guys don't mind, tells us how we can improve our question asking skills.
"I am trying to use my API to assign the account we are logging in as with a token. I've tried various things, but nothing really seems to work. What needs to be sent to the URL for the token to be generated looks like this:
{
"grant_type": "password",
"username": "user#example.com",
"password": "password"
}
Then we need to be able to receive the token so that we can do some things with that account. Could anybody push me in the right direction to accomplish this? Also want to add - do we need a model and collection for our token?
Our Goal is to be able to retrieve a token, so that after we create a user we can login into our app on the login page.
// User Model and Collection. Used to input user data.
var User = Backbone.Model.extend({
url: "https://twitterapii.herokuapp.com/users"
});
//Collection for when we GET data from API
var Users = Backbone.Collection.extend({
model: User,
url: "https://twitterapii.herokuapp.com/users",
parse: function(response) {
return response.data;
}
})
//Collection for when we GET data from API
var UsersTokens = Backbone.Collection.extend({
model: User,
url: "https://twitterapii.herokuapp.com/oauth/token",
parse: function(response) {
return response.data;
}
})
var signinView = Backbone.View.extend({
tagName: 'section',
template: _.template($('#signInTemplate').html()),
events: {
"click .signinButton": 'handleLoginClick'
},
send: function(){
var signin = this.$('.signinButton').val();
console.log("clicking!!");
var email = this.$(".email").val();
var password = this.$(".password").val();
logUser: UsersTokens.fetch( {
headers: {
grant_type: password,
username: email,
password: password
} });
},
render: function(){
this.$el.html(this.template());
},
handleLoginClick: function(){
event.preventDefault();
this.send();
console.log("login clicksss!!");
}
});

Angularjs response not printing in form

I am trying to print the response from my controller to my form. But it is not printing the messages passed from it.
Here is my controller .js file
app.controller('regCtrl', function ($scope) {
});
$scope.doRegister = function ()
{
username = $scope.signup.registerUserName;
email = $scope.signup.email;
password = $scope.signup.registerPassword;
$http({
url: 'app/toolkit/calls/doRegister.php',
method: "GET",
params: {username: username, email: email, password: password}
}).success(function (status, data, response, header) {
$scope.data.msg = 'Account not Activated'
});
};
and in the form i have
<div ng-controller="regCtrl">
<div ng-if="data.show" ng-bind="data.msg"></div>
What is the mistake i am doing and how can i make the response messages print in the form.
I fixed it by turning it the $scope.data true by
$scope.data = {show: true};
then it works.

Cakephp Ajax Login is loggin in even with wrong data

I have a simple html form to login outside my cake structure, I send the login data via jquery to my login function:
public function login() {
if ($this->request->is('ajax')) {
$this->request->data['Appuser']['password'] = Security::hash($this->request->data['password']);
$this->request->data['Appuser']['name'] = $this->request->data['name'];
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl(array('action' => 'returnUserData')));
}else{
$this->response->body(json_encode('Login failed!'));
}
}elseif($this->request->is('post')){
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}else{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
The very strange thing is:
the login via post from /view/users/login.ctp form works perfectly!
When I try to login from "outside" via ajax, I am always logged in even with wrong access details and get the data from function 'returnUserData'
$('#login').click(function() {
$.ajax({
type: "POST",
url: '/api/login',
data: {
name: $("#username").val(),
password: $("#password").val()
},
success: function(data)
{
alert(data);
}
});
});
When I set a debug(); after if ($this->request->is('ajax')) I can see it, so the script seems to go into the right if section. But I don't get it, why the $this->Auth->login always return true...?

Categories

Resources