Flutter Amplify Cognito, no tokens using fetchAuthSession - flutter

I'm trying to implement authentication in my Flutter app using Cognito. I'm authenticating against an existing userPool which I've been successfully using for the past year in my React app.
However, with Flutter I'm not able to fetch the user's session. I'm able to login successfully but I'm unable to get any tokens using the fetchAuthSession() method. Any idea why this is happening? Here is some of my working and non-working code:
This code is successful...
Future _usersEmail() async {
try {
var attributes = (await Amplify.Auth.fetchUserAttributes()).toList();
for (var attribute in attributes) {
if (attribute.userAttributeKey == 'email') {
print("user's email is ${attribute.value}");
return '${attribute.value}';
}
}
return 'no email';
} on AuthException catch (e) {
return '${e.message}';
}
}
This code is successful too...
Future<bool> _isSignedIn() async {
final CognitoAuthSession session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
print('_isSignedIn: ${session.isSignedIn}');
return session.isSignedIn;
}
This code return null...
Future _getIdToken() async {
final CognitoAuthSession session =
await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
final idToken = session.userPoolTokens?.idToken;
print('idToken: $idToken');
return idToken;
}
Here is my amplifyconfig...
{
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
"UserAgent": "aws-amplify-cli/0.1.0",
"Version": "0.1.0",
"IdentityManager": {
"Default": {}
},
"CredentialsProvider": {
"CognitoIdentity": {
"Default": {
"PoolId": "us-east-1_abcxyz",
"Region": "us-east-1"
}
}
},
"CognitoUserPool": {
"Default": {
"PoolId": "us-east-1_abcxyz",
"AppClientId": "5j0kii90dJ09est43xh3X21",
"Region": "us-east-1"
}
},
"Auth": {
"Default": {
"authenticationFlowType": "USER_SRP_AUTH"
}
}
}
}
}
}

you might need to set getAWSCredentials to true in your options parameter like so:
final authSession = (await Amplify.Auth.fetchAuthSession(
options: CognitoSessionOptions(getAWSCredentials: true),
)) as CognitoAuthSession;

Related

Always get when Check whether the given values are correct and the user is authorized to perform the operation swift amplify custom auth

I'm new to AWS Amplify. I'm trying to implement Amplify Auth in Swift. I've written the code for Login. Sign-up will be handled on the admin panel, and we will be using passwordless authentication. User will enter their email for login and OTP will be sent to email and login will be confirmed. I have called amplify.configure() and AWSCognitoAuthPlugin. But I have been stuck on an error
I have imported the Amplify UserPool with Amplify CLI.
func amplifyConfig() {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.configure()
print("Amplify configured with auth plugin")
} catch {
print("An error occurred setting up Amplify: \(error)")
}
}
func signInWithEmail(email: String, completion: #escaping (MyResult<AuthStep, AuthError>) -> Void) {
let customAuth = AWSAuthSignInOptions(authFlowType: .custom)
Amplify.Auth.signIn(username: email, options: .init(pluginOptions: customAuth)) { (result) in
print(result)
switch result {
case .success(let result):
self.updateCurrentUser()
completion(.success(result.nextStep.authStep))
case .failure(let error):
completion(.failure(error))
}
}
}
Error Returned:
failure(AuthError: Incorrect username or password.
Recovery suggestion: Check whether the given values are correct and the user is authorized to perform the operation.)
amplifyconfiguration.json file
{
"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
"UserAgent": "aws-amplify/cli",
"Version": "0.1.0",
"IdentityManager": {
"Default": {}
},
"CognitoUserPool": {
"Default": {
"PoolId": "us-east-*******",
"AppClientId": "**************40i8",
"Region": "us-east-2"
}
},
"Auth": {
"Default": {
"OAuth": {
"WebDomain": "********-east-2.amazoncognito.com",
"AppClientId": "******************",
"SignInRedirectURI": "https://www.purelogics.net/",
"SignOutRedirectURI": "https://www.purelogics.net/",
"Scopes": [
"aws.cognito.signin.user.admin",
"email",
"openid"
]
},
"authenticationFlowType": "CUSTOM_AUTH",
"socialProviders": [
"GOOGLE"
],
"usernameAttributes": [
"EMAIL"
],
"signupAttributes": [
"NAME",
"EMAIL"
],
"passwordProtectionSettings": {
"passwordPolicyMinLength": 8,
"passwordPolicyCharacters": []
},
"mfaConfiguration": "OFF",
"mfaTypes": [],
"verificationMechanisms": []
}
}
}
}
}
}
I think this code should work fine. Kindly let me know if this issue is from my side or in LAMBDA function or something.

flutter login with dio package

i try to login to the api with dio and i cant handle it
my scenario is:
login with user name,password and server returned session like this:
{
"function": "request_session_generate",
"params": {
"username": "myuser",
"password": "mypass"
}
}
and when send request post method returned session like below code:
{
"result": 0,
"data": {
"session": "mysession...",
"session_status": "generated"
}
}
and second post method for validate this session ... put the session and otp code(google authenticator) like this:
{
"function": "request_session_validate",
"params": {
"session": "mysession",
"otp": "123456"
}
}
when put the session in to the session params server returned:
{
"result": 0,
"data": {
"session": "newSession",
"session_status": "validated"
}
}
how can get the session in the body response first method and use it in the second function for validate
my first function implement code :
class ApiClient {
final Dio _dio = Dio();
Future<Response?> login() async {
try {
Response response = await _dio.post(
'myserver/api',
options: Options(
headers: {
"apikey": "00000000-0000-0000-0000-000000000000",
},
),
data: {
"function": "request_session_generate",
"params": {
"username": "myuser",
"password": "mypass"
}
},
);
//returns the successful user data json object
if(response.statusCode == 200){
return response.data;
}
} on DioError catch (e) {
//returns the error object if any
return e.response!.data;
}
return null;
}
and my implemention for validate and not worked:
Future<Response?> validate() async {
try {
Response responsevalidate = await _dio.post(
'myserver/api',
data: {
"function": "request_session_validate",
"params": {
"session": "mysession",
"otp": "123456"
}
},
);
//returns the successful user data json object
return responsevalidate.data;
} on DioError catch (e) {
//returns the error object if any
return e.response!.data;
}
//IMPLEMENT USER LOGIN
}
how can get session in first function and use it in second function for validate?
use the json decode or something else ?
thank you for help me

Why do I keep getting this error when I try to connect mongodb to my next.js project

I keep getting this error when I try to connect my mongodb to my user api route file. I don't know what is going wrong and I do not know how to solve it, but I'm sure this is what my teacher wrote on his computer and it worked without any errors.
This is what my dbconnection file looks like
// MongoDB Connection
import mongoose from "mongoose";
if (!process.env.MONGODB_URL) {
throw new Error("Please define the MONGODB_URL environment variable inside .env.local");
}
let cached = global.mongoose;
if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
async function dbConnect() {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
};
cached.promise = mongoose.connect(process.env.MONGODB_URL, opts).then((mongoose) => {
return mongoose;
});
}
cached.conn = await cached.promise;
return cached.conn;
}
export default dbConnect;
And this is how I define my user routes to the /api/user endpoint
import Users from "../../../api/models/Users";
import dbConnect from "../../util/mongo";
export default async function handler(req, res) {
const { method } = req;
dbConnect()
switch (method) {
case "GET":
try {
const res = await Users.find();
res.status(200).json(res);
} catch (error) {
res.status(500).json(error);
}
break;
case "POST":
console.log(POST);
break;
case "PUT":
console.log(PUT);
break;
case "Delete":
console.log(Delete);
break;
}
}
my package.json file is thus:
"name": "smooon",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"axios": "^1.1.0",
"mongodb": "^3.7.3",
"mongodb-client-encryption": "^2.3.0",
"mongoose": "^6.6.5",
"next": "12.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-snowfall": "^1.2.1",
"react-spinners-kit": "^1.9.1",
"react-tinder-card": "^1.5.4"
},
"devDependencies": {
"eslint": "8.23.1",
"eslint-config-next": "12.3.1"
}
}

How to handle errors via Ferry GraphQL

community!
Please give a clue how to handle GraphQL errors by Ferry.
To simplify let's say that I have toLogin function:
await toLogin(_email, _password);
and it initiates login procedure:
Future<void> sendLogin(String _email, String _password) async {
final _sendLoginRequest = GLoginReq((b) => b
..vars.identifier = _email
..vars.password = _password);
final _client = initClient();
final _storage = GetStorage();
_client.request(_sendLoginRequest).listen((response) {
if (response.hasErrors) {
print('error');
} else {
print('success');
}
});
}
Works fine but I need to add error handling at the main program.
so I add
String _result = await toLogin(_email, _password);
to the main and return to the toLogin();
Future<String> sendLogin(String _email, String _password) async {
final _sendLoginRequest = GLoginReq((b) => b
..vars.identifier = _email
..vars.password = _password);
final _client = initClient();
String _result = 'init';
_client.request(_sendLoginRequest).listen((response) {
if (response.hasErrors) {
print('error');
_result = 'error';
} else {
print('success');
_result = 'success';
}
});
return _result;
}
The problem is that it returns _result before any check and Dart do not allow me to use await
OK, let's try another way:
var _response = OperationResponse(
operationRequest: _sendLoginRequest,
);
var _checkErrors = _response.graphqlErrors;
print(_checkErrors);
But it's always false
I've found option graphqlErrors at OperationResponse but do not have any idea what to fill there.
Please give me a clue how to handle it.
I use Strapi as the backend, the error example is as follows:
{
"errors": [
{
"message": "Bad Request",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"login"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"code": 400,
"data": {
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"messages": [
{
"id": "Auth.form.error.invalid",
"message": "Identifier or password invalid."
}
]
}
],
"data": [
{
"messages": [
{
"id": "Auth.form.error.invalid",
"message": "Identifier or password invalid."
}
]
}
]
},
"stacktrace": [
"Error: Bad Request",
" at checkBadRequest (C:\\Projects\\Zhugory\\zhugory-strapi-graphql\\zhugory-strapi\\node_modules\\strapi-plugin-users-permissions\\config\\schema.graphql.js:13:23)",
" at resolver (C:\\Projects\\Zhugory\\zhugory-strapi-graphql\\zhugory-strapi\\node_modules\\strapi-plugin-users-permissions\\config\\schema.graphql.js:217:11)",
" at runMicrotasks (<anonymous>)",
" at processTicksAndRejections (internal/process/task_queues.js:93:5)"
]
}
}
}
],
"data": null
}

Storing data "across conversations" in Google Action

I'm a bit confused by the Google Actions documentation about storing data and hoped someone can help clarify...
The docs state that data in the conv.user.storage object will be saved "across conversations". I took this to mean that if the user exited the conversation these values would be persisted and available the next time they interact with my action. Is that understanding correct?
The reason I ask is that I can't get this behaviour to work in my action.
I have built a simple action fulfilment service (using Actions on Google NodeJS library v2.4.0 and Koa v2.5.3). The fulfilment is triggered from an intent defined in Dialogflow (after an account has been linked with Google Sign In) and stores a value in conversation storage. The code is as follows:
server.js (base server - loads actions dynamically from the local ./actions/ dir)
/* Load the environment */
const dotenv = require('dotenv');
const path = require('path');
const packageJson = require('./package.json');
dotenv.config({
silent: true,
path: process.env.ENV_FILE!=undefined && process.env.ENV_FILE.trim()!='' ? path.normalize(process.env.ENV_FILE) : path.join(__dirname, './.env')
});
const SERVER_NAME = process.env.NAME || packageJson.name;
const SERVER_PORT = process.env.PORT||'8080';
const SERVER_HOST = process.env.HOST||'0.0.0.0';
const HANDLERS_PATH = './actions/';
/* Load the dependencies */
const logger = require('utils-general').logger('google-server');
const Koa = require('koa');
const KoaBody = require('koa-body');
const KoaActionsOnGoogle = require('koa-aog');
const fs = require('fs');
const { dialogflow } = require('actions-on-google');
/* Load and initialise the Google Assistant actions */
//Initialise DialogFlow
const action = dialogflow({ debug: process.env.ACTIONS_DEBUG==='true', clientId: process.env.GOOGLE_CLIENT_ID });
//Load the action intent handlers
const handlers = [];
let handlerFiles = fs.readdirSync(HANDLERS_PATH);
handlerFiles.forEach(function loadHandlers(file) {
let handlerImpl = require(HANDLERS_PATH+file);
let handler = {};
handler[handlerImpl.intent] = handlerImpl.action;
handlers.push(handler);
});
//Add the actions intent handlers to DialogFlow
handlers.forEach(item => {
let key = Object.keys(item)[0];
logger.info(`Adding handler for action intent ${key}`);
action.intent(key, item[key]);
});
/* Create the application server to handle fulfilment requests */
logger.info(`Initialising the ${SERVER_NAME} server (port: ${SERVER_PORT}, host: ${SERVER_HOST})`);
//Create the server
const app = new Koa();
//Add default error handler middleware
app.on('error', function handleAppError(err) {
logger.error(`Unhandled ${err.name||'Error'}: ${err.message || JSON.stringify(err)}`);
});
//Add body parsing middleware
app.use(KoaBody({ jsonLimit: '50kb' }));
//Log the request/ response
app.use(async (ctx, next) => {
logger.trace(`REQUEST ${ctx.method} ${ctx.path} ${JSON.stringify(ctx.request.body)}`);
await next();
logger.trace(`RESPONSE (${ctx.response.status}) ${ctx.response.body ? JSON.stringify(ctx.response.body) : ''}`);
});
//Make the action fulfilment endpoint available on the server
app.use(KoaActionsOnGoogle({ action: action }));
/* Start server on the specified port */
app.listen(SERVER_PORT, SERVER_HOST, function () {
logger.info(`${SERVER_NAME} server started at ${new Date().toISOString()} and listening for requests on port ${SERVER_PORT}`);
});
module.exports = app;
storage-read.js (fulfilment for the "STORAGE_READ" intent - reads stored uuid from conversation storage):
const logger = require('utils-general').logger('google-action-storage-read');
const { SimpleResponse } = require('actions-on-google');
const { getUserId } = require('../utils/assistant-util');
const _get = require('lodash.get');
module.exports = {
intent: 'STORAGE_READ',
action: async function (conv, input) {
logger.debug(`Processing STORAGE_READ intent request: ${JSON.stringify(conv)}`, { traceid: getUserId(conv) });
let storedId = _get(conv, 'user.storage.uuid', undefined);
logger.debug(`User storage UUID is ${storedId}`);
conv.close(new SimpleResponse((storedId!=undefined ? `This conversation contains stored data` : `There is no stored data for this conversation`)));
}
}
storage-write.js (fulfils the "STORAGE_WRITE" intent - writes a UUID to conversation storage):
const logger = require('utils-general').logger('google-action-storage-read');
const { SimpleResponse } = require('actions-on-google');
const { getUserId } = require('../utils/assistant-util');
const _set = require('lodash.set');
const uuid = require('uuid/v4');
module.exports = {
intent: 'STORAGE_WRITE',
action: async function (conv, input) {
logger.debug(`Processing STORAGE_WRITE intent request`, { traceid: getUserId(conv) });
let newId = uuid();
logger.debug(`Writing new UUID to conversation storage: ${newId}`);
_set(conv, 'user.storage.uuid', newId);
conv.close(new SimpleResponse(`OK, I've written a new UUID to conversation storage`));
}
}
This "STORAGE_WRITE" fulfilment stores the data and makes it available between turns in the same conversation (i.e. another intent triggered in the same conversation can read the stored data). However, when the conversation is closed, subsequent (new) conversations with the same user are unable to read the data (i.e. when the "STORAGE_READ" intent is fulfilled) - the conv.user.storage object is always empty.
I have voice match set up on the Google account/ Home Mini I'm using, but I can't see how I determine in the action if the voice is matched (although it seems to be as when I start a new conversation my linked account is used). I'm also getting the same behaviour on the simulator.
Sample request/ responses (when using the simulator) are as follows:
STORAGE_WRITE request:
{
"user": {
"userId": "AB_Hidden_EWVzx3q",
"locale": "en-US",
"lastSeen": "2018-10-18T12:52:01Z",
"idToken": "eyMyHiddenTokenId"
},
"conversation": {
"conversationId": "ABwppHFrP5DIKzykGIfK5mNS42yVzuunzOfFUhyPctG0h0xM8p6u0E9suX8OIvaaGdlYydTl60ih-WJ5kkqV4acS5Zd1OkRJ5pnE",
"type": "NEW"
},
"inputs": [
{
"intent": "actions.intent.MAIN",
"rawInputs": [
{
"inputType": "KEYBOARD",
"query": "ask my pathfinder to write something to conversation storage"
}
],
"arguments": [
{
"name": "trigger_query",
"rawText": "write something to conversation storage",
"textValue": "write something to conversation storage"
}
]
}
],
"surface": {
"capabilities": [
{
"name": "actions.capability.WEB_BROWSER"
},
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
},
{
"name": "actions.capability.MEDIA_RESPONSE_AUDIO"
}
]
},
"isInSandbox": true,
"availableSurfaces": [
{
"capabilities": [
{
"name": "actions.capability.WEB_BROWSER"
},
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
}
]
}
],
"requestType": "SIMULATOR"
}
STORAGE_WRITE response:
{
"conversationToken": "[]",
"finalResponse": {
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "OK, I've written a new UUID to conversation storage"
}
}
]
}
},
"responseMetadata": {
"status": {
"message": "Success (200)"
},
"queryMatchInfo": {
"queryMatched": true,
"intent": "a7e54fcf-8ff1-4690-a311-e4c6a8d1bfd7"
}
},
"userStorage": "{\"data\":{\"uuid\":\"7dc835fa-0470-4028-b8ed-3374ed65ac7c\"}}"
}
Subsequent STORAGE_READ request:
{
"user": {
"userId": "AB_Hidden_EWVzx3q",
"locale": "en-US",
"lastSeen": "2018-10-18T12:52:47Z",
"idToken": "eyMyHiddenTokenId"
},
"conversation": {
"conversationId": "ABwppHHVvp810VEfa4BhBJPf1NIfKUGzyvw9JCw7kKq9YBd_F8w0VYjJiSuzGLrHcXHGc9pC6ukuMB62XVkzkZOaC24pEbXWLQX5",
"type": "NEW"
},
"inputs": [
{
"intent": "STORAGE_READ",
"rawInputs": [
{
"inputType": "KEYBOARD",
"query": "ask my pathfinder what is in conversation storage"
}
],
"arguments": [
{
"name": "trigger_query",
"rawText": "what is in conversation storage",
"textValue": "what is in conversation storage"
}
]
}
],
"surface": {
"capabilities": [
{
"name": "actions.capability.WEB_BROWSER"
},
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
},
{
"name": "actions.capability.MEDIA_RESPONSE_AUDIO"
}
]
},
"isInSandbox": true,
"availableSurfaces": [
{
"capabilities": [
{
"name": "actions.capability.WEB_BROWSER"
},
{
"name": "actions.capability.AUDIO_OUTPUT"
},
{
"name": "actions.capability.SCREEN_OUTPUT"
}
]
}
],
"requestType": "SIMULATOR"
}
STORAGE_READ response:
{
"conversationToken": "[]",
"finalResponse": {
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "There is no stored data for this conversation"
}
}
]
}
},
"responseMetadata": {
"status": {
"message": "Success (200)"
},
"queryMatchInfo": {
"queryMatched": true,
"intent": "368d08d3-fe0c-4481-aa8e-b0bdfa659eeb"
}
}
}
Can someone set me straighten me out on whether I'm misinterpreting the docs or maybe I have a bug somewhere?
Thanks!
my suspicion is that personal results are turned off in your case.
You mentioned you're testing on Home Mini and Prisoner was able reproduce on device (in the comments).
Shared devices like Smart Speakers (Home, Mini) and Smart Displays have personal results disabled by default. Check this documentation to enable it.
Open Settings on your Android phone
Under "Assistant devices," select your device (e.g. Mini)
Turn Personal results on
Beware that this means personal results like Calendar entries can be accessed through the device.
To check if userStorage will persist, you can use the GUEST/VERIFIED flag, see documentation here.