google login authentication in ionic 2
with step by step.
i try to print simple response but it does not work
GLogin(){
alert("you are in google plush ");
GooglePlus.login((res)=>{
alert(this.data=res);
});
}
Add the plugin (check this link):
cordova plugin add cordova-plugin-googleplus --save --variable REVERSED_CLIENT_ID=myreversedclientid
Include the lib:
import { GooglePlus } from 'ionic-native';
Now the login:
googlePlus_login() {
GooglePlus.login(
{
'scopes': '',
'webClientId': '',
'offline': false
}
).then(
(success) => {
alert( '\n id: ' + JSON.stringify(success.userId) +
'\n name: ' + JSON.stringify(success.displayName) +
'\n email: ' + JSON.stringify(success.email)
);
},
(failure) => {
console.log('GOOGLE+ login FAILED', failure);
}
);
}
where:
scope: optional, space-separated list of scopes, If not included or empty, defaults to profile and email.
webClientId: optional clientId of your Web application from Credentials settings of your project - On Android, this MUST be included to get an idToken. On iOS, it is not required.
offline: optional, but requires the webClientId - if set to true the plugin will also return a serverAuthCode, which can be used to grant offline access to a non-Google server.
Logout function:
googlePlus_logout() {
GooglePlus.logout().then(
(success) => {
console.log('GOOGLE+: logout DONE', success);
},
(failure) => {
console.log('GOOGLE+: logout FAILED', failure);
}
);
}
Hope it will help you. :)
Related
I have created a Cypress command to fetch me a JWT token from my GQL API. I then set the token as a cookie to use in my tests that require authentication.
Here is my login command:
export function login(username, password) {
const { apiUrl: url } = Cypress.env();
cy.request({
method: 'POST',
url,
body: {
operationName: 'signin',
headers: {
'content-type': 'application/json'
},
query:
`mutation signin($email: String!, $password: String!) {
signin(input: {email: $email, password: $password}) {
id
token
__typename
}
}`,
variables: { email: username, password: password }
}
})
.then(response => {
const { token } = response.body.data.signin;
cookie.set('token', token, { expires: 10000000 });
cy.wait(3000);
});
}
I can see when I run the login command the cookie is set but when my test tries to visit a page within my app the cookie disappears.
describe('Entity Page', () => {
before(() => {
const { username, password } = Cypress.env();
cy.login(username, password);
cy.addEntity(newEntityId, {});
cy.wait(3000);
cy.get('#entityId').then(entityId => {
cy.visit(`/entity/${entityId}`)
cy.wait(6000)
});
});
By the time I get to addEntity the cookie disappears and I am unauthenticated. Is there something I need to do to persist cookies? I tried Cypress.Cookies.preserveOnce but this had no effect
I also tried adding the below to my support/index.js but the cookie is still removed.
Cypress.Cookies.defaults({
preserve: 'token'
})
Try it with cy.setCookie(name, value) docs.
It has a couple of defaults that might help.
domain - defaults to window.location.hostname. The domain the cookie is visible to.
expiry - defaults to 20 years into the future. Specified as seconds since 1 January 1970 (10,000,000 is only 115 days).
I have followed the following steps in order to get Amplify Auth login flow working on React Native:
created project with Expo, ejected to ExpoKit
yarn add aws-amplify, yarn add aws-amplify-react-native
react-native link
amplify init
amplify configure
amplify add auth
amplify push
The React Native app that I am running consists of a main App.js component with the following imports:
import React, { Component } from "react";
import { StyleSheet, View, Text } from "react-native";
import Amplify, { Auth } from "aws-amplify";
import AWSConfig from "./aws-exports";
Amplify.configure(AWSConfig);
import Tabs from "./Tabs";
My main App.js file also has two tabs (one for SignIn component and another for SignUp component).
My SignUp component looks like this:
...
signUp = () => {
Auth.signUp({
username: this.state.username,
password: this.state.password,
attributes: {
email: this.state.email
}
})
.then(() => console.warn("successful sign up!"))
.catch(err => console.warn("error signing up!: ", err));
};
confirmSignUp = () => {
Auth.confirmSignUp(this.state.username, this.state.confirmationCode)
.then(() => console.warn("successful confirm sign up!"))
.catch(err => console.warn("error confirming signing up!: ", err));
};
render() {
return (
<View style={styles.container}>
...
<Button title="Confirm Sign Up" onPress={this.confirmSignUp} />
</View>
);
}
...
My problem is that when I try to sign up a user then I get:
attribute value for phone number must not be null
When I check the attributes of the User Pool that was automatically created at: AWS cognito console then
email
is the only "standard attribute that is required".
Please advise.
You can do something like this:
In your component class initialise state to remember the 'email_address' and 'phone_number' inputs, and make sure to set the "onChange" function of the textInput to "this.setState({phone_number: value})"
`class myClass extends React.component{
constructor(props){
super(props)
this.state={(email_address = undefined;
phone_number= undefined;)}
}
... //Same code here
signUp = () => {
Auth.signUp({
username: this.state.username,
password: this.state.password,
attributes: {
email: this.state.email
phone_number: this.state.phone_number
}
})
.then(() => console.warn("successful sign up!"))
.catch(err => console.warn("error signing up!: ", err));
};
....//Some code
}`
I'm trying to make Http requests using Angular 6. My login call works, but when I try to get use a different call, it tells me I'm not logged in. I think it's because the login isn't valid, but I'm not sure how I can keep it valid for subsequent calls. Here is the code appComponent file:
ngOnInit(): void {
this.data = this.login.getData();
this.farmAccessdata = this.getFarmAccess.farmAccess();
}
And here is the login service:
export class loginService {
base_URL = "..."
login = {
username: username,
password: password
}
constructor(private http: HttpClient) {
}
getData(){
return this.http.post(this.base_URL + "...", JSON.stringify(this.login))
.subscribe(data => {
console.log("We got ", data)
})
}
And the farmaccess service:
export class GetFarmAccessService {
data = {};
baseURL = "..."
constructor(private http: HttpClient) { }
farmAccess(){
return this.http.get(this.baseURL + "...")
.subscribe(data => {
console.log("We got ", data)
})
}
When I run the farmAccess service, it gives me an error saying I'm not logged in. The login framework on the server side is cookie based auth, powered by django user module. How can I fix this? Thanks.
I have implemented the facebook login for my ionic application, which works perfectly when run on web. When i build the application, create an apk of the same, and try to run on my mobile device, nothing happens.
The login is:
openFB.login(
function (response) {
if (response.status === 'connected') {
console.log('Facebook login succeeded, got access token: ', response);
openFB.api({
path: '/me',
success: function (data) {
console.log("My Data", data);
userData.name = data.name;
userData.picture = 'http://graph.facebook.com/' + data.id + '/picture?type=small';
localStorageService.set('user', userData);
$timeout(function() {
$state.go('app.home');
}, 0);
},
error: function(error) {
console.log("Error here:", error);
}
});
} else {
console.log('Facebook login failed: ' + response);
}
}, { scope: 'email, public_profile' });
Have used openFB for the login. After clicking, following popup comes up.
After clicking the okay, nothing gets logged. No console message.
Can some one help me for finding out this issue, where i am not able to do the facebook login, when run on actual device.
You need to whitelist the redirect url. You can set it in
Products > Facebook Login > Settings > Client OAuth Settings
Take a look into this question.
please set redirect URI in
Products > Facebook Login > Settings > Client OAuth Settings
http://localhost/callback
please follow the below procedure to register your app in facebook developer site
https://ccoenraets.github.io/ionic-tutorial/ionic-facebook-integration.html
and use the below code to complete the procedure of facebook login
$cordovaOauth.facebook("appId", ["email", "public_profile"]).then(function(result) {
//alert(JSON.stringify(result));
//$localStorage.accessToken = result.access_token;
$http.get("https://graph.facebook.com/v2.2/me", {
params: {
access_token: result.access_token,
fields: "id,name,gender,location,email,picture,relationship_status",
format: "json"
}
}).then(function(result) {
// alert(JSON.stringify(result));
$scope.loginflowusingsociallogin(result.data.email);
}, function(error) {
alert("There was a problem getting your profile. Check the logs for details.");
alert(JSON.stringify(error));
});
});
i used Oauth 2.0 authentication for ionic.
I used this code and worked fine for me
I am working on a Facebook canvas iFrame application, and I`m going insane.
I am trying to check if a user is a fan of the page where the app is located, so that I can allow or disallow voting.
I use the following code:
function CheckFan() {
FB.init({
appId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
FB.api({ method: 'pages.isFan', page_id: '145116742177104' }
, function(resp) {
if (resp) { $('#main_frame').show(); $('#non_fan').hide(); }
else { $('#main_frame').hide(); $('#non_fan').show(); }
});
}
This JS SDK is driving me up the wall, while calling the documentation "incomplete" is an insult to incompleteness.
Any input will be appriciated.
Thank you!
-Elad
This has been deprecated by Facebook.
A new Graph API alternative will hopfuly be available by the time we need to deploy the app.
For now I use FQL:
FB.api({ method: 'fql.query', query: 'SELECT uid FROM page_fan WHERE uid= ' + user_id + ' AND page_id=145116742177104' },
function(result) {
if (result.length)
{ $('.main_frame').show(); $('#non_fan').hide(); } else { $('.main_frame').hide(); $('#non_fan').show(); }
});