quickblox login authentication error in swift - quickblox

i am able to sign up but unble to log in in swift.
#IBAction func loginUser(_ sender : UIButton){
QBRequest.logIn(withUserLogin: txtUserName.text!, password: txtPassword.text!, successBlock: { (response, user) in
print("created user ::::",user)
}) { (error) in
print("created error")
}
}
i am getting below error.
error: Request failed: unauthorized (401)
reasons: {
errors = (
"Unauthorized"
);
}

As Described in
https://quickblox.com/developers/Errors
You Might have entered Wrong Email/Password Or You Probably Using Invalid Token.

Related

Not being able to read statusCode when sending a request

I am developing an api with node.js but i am having trouble with one of my routers while sending a request in postman.
The following is my router:
//#route GET api/profile/github/:username
//#desc Get user repos from github
//#access public
router.get('/github/:username', (req,res)=>{
try {
const options = {
uri: `https://api/github.com/users/${req.params.username}/repos?per_page=5&sort=created:asc&client_id=${config.get('githubClientId')}&client_secret=${config.get('githubSecret')}`,
method:'GET',
headers:{'user_agent': 'node.js'}
};
request(options, (error, response, body) => {
if(error) console.error(error);
if(response.statusCode !== 200){
res.status(404).json('No Github profile found');
}
res.json(JSON.parse(body));
});
} catch (error) {
console.error(error.message);
res.status(500).send('Server Error');
}
})
So in this route i am trying to find a github username that is being passed via the uri.
This is the request i am sending:
http://localhost:5000/api/profile/github/<GITHUB_USERNAME>
but when i send my request i get the following error in my VSC console.
Cannot read properties of undefined (reading 'statusCode')
if(response.statusCode !==200){
There are a couple issues here:
First, https://api/github.com/users/xxx should be https://api.github.com/users/xxx. You put a / where there should have been a . .
And, in fact, your very code should be showing the error:
Error: getaddrinfo ENOTFOUND api
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'api'
}
which is telling you that the domain api was not found. It's looking for that domain because of the error in your URL.
Second, this error is complicated in your code because if you get an error returned from the request() library, then the callback arguments response and body are invalid, but you try to use them. The physical request did not succeed so there is no response or body.
You can amend your code like this:
router.get('/github/:username', (req, res) => {
try {
const options = {
uri: `https://api/github.com/users/${req.params.username}/repos?per_page=5&sort=created:asc&client_id=${config.get('githubClientId')}&client_secret=${config.get('githubSecret')}`,
method: 'GET',
headers: { 'user_agent': 'node.js' }
};
request(options, (error, response, body) => {
if (error) {
console.error(error);
res.status(500).json({code: error.code, message: "Network error communicating with github"});
return;
}
if (response.statusCode !== 200) {
res.status(404).json('No Github profile found');
return;
}
res.json(JSON.parse(body));
});
} catch (error) {
console.error(error.message);
res.status(500).send('Server Error');
}
});
This handles the error by returning a 500 status and creating an error object to send back to the client. It also adds return statements after sending a response so the following code doesn't execute and try to send other responses.
NOTE: The request() library has been deprecated since early 2020 and will no longer be developed with new features. It is NOT recommended for new code. You can use newer, promise-based libraries such as node-fetch, got or any of the others listed here. In the most recent versions of nodejs, you can use the built-in version of fetch.

How to properly call and implement ConnectyCube's External auth via Custom Identity Provider

I'm trying to implement Custom Identity Provider of ConnectyCube.
The instructions are:
I am now trying to implement the last step:
POST https://api.connectycube.com/login
login=IP_user_token
I try to do the above by the following code:
const login = {
login: token,
};
try {
const {data} = await axios.post(
`https://api.connectycube.com/login`,
login,
);
console.log('data', data);
} catch (err) {
console.log('err while calling api.connectycube.com/login err', err);
}
When I do that though I get the following 403 error:
[Error: Request failed with status code 403]
Am I POSTing incorrectly?
How to fix?

How do I return an error from a Controller in Loopback 4?

I have a controller method
// ... inside a controller class
#get('/error', {})
async error() {
throw new Error("This is the error text");
}
The response I'm getting from this error front-end is:
{
"error": {
"statusCode": 500,
"message": "Internal Server Error"
}
}
What I would like the error to be is:
{
"error": {
"statusCode": 500,
"message": "This is the error text"
}
}
How do I return an error from a controller in Loopback 4?
Hello from the LoopBack team 👋
In your controller or repository, you should throw the Error exactly as shown in your question.
Now when LoopBack catches an error, it invokes reject action to handle it. The built-in implementation of reject logs a message via console.error and returns an HTTP response with 4xx/5xx error code and response body describing the error.
By default, LoopBack hides the actual error messages in HTTP responses. This is a security measure preventing the server from leaking potentially sensitive data (paths to files that could not be opened, IP addresses of backend service that could not be reached).
Under the hood, we use strong-error-handler to convert Error objects to HTTP responses. This module offers two modes:
Production mode (the default): 5xx errors don't include any additional information, 4xx errors include partial information.
Debug mode (debug: true): all error details are included on the response, including a full stack trace.
The debug mode can be enabled by adding the following line to your Application constructor:
this.bind(RestBindings.ERROR_WRITER_OPTIONS).to({debug: true});
Learn more in our docs: Sequence >> Handling errors
Alternatively, you can implement your own error handler and bind it as the sequence action reject. See Customizing sequence actions in our docs.
export class MyRejectProvider implements Provider<Reject> {
constructor(
#inject(RestBindings.SequenceActions.LOG_ERROR)
protected logError: LogError,
#inject(RestBindings.ERROR_WRITER_OPTIONS, {optional: true})
protected errorWriterOptions?: ErrorWriterOptions,
) {}
value(): Reject {
return (context, error) => this.action(context, error);
}
action({request, response}: HandlerContext, error: Error) {
const err = <HttpError>error;
const statusCode = err.statusCode || err.status || 500;
const body = // convert err to plain data object
res.statusCode = statusCode;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(JSON.stringify(body), 'utf-8');
this.logError(error, statusCode, request);
}
}
If you just want to show error message, you just extend Error object and throw it like below. (Loopback documentation didn't mention this anyway)
Avoid using 5xx error and use 4xx error to show some important thing to user is best practice and so that Loopback4 was implemented like this.
class NotFound extends Error {
statusCode: number
constructor(message: string) {
super(message)
this.statusCode = 404
}
}
...
if (!await this.userRepository.exists(id)) {
throw new NotFound('user not found')
}
For my situation, I found a catch in my sequence.ts file. Inside the catch, it checked if the error had a status code of 4xx, and if not, it just returned a anonymous 500.
Here's the code I was looking for to do the logic:
// sequence.ts
...
} catch (err) {
console.log(err);
let code: string = (err.code || 500).toString();
if (code.length && code[0] === '4') {
response.status(Number(code) || 500);
return this.send(response, {
error: {
message: err.message,
name: err.name || 'UnknownError',
statusCode: code
}
});
}
return this.reject(context, err);
}
...
Here's how you tell it what to do:
// ... inside a controller class
#get('/error', {})
async error() {
throw {
code: 400,
message: "This is the error text",
name: "IntentionalError"
}
}
To throw custom validation error I use this method:
private static createError(msg: string, name?: string): HttpErrors.HttpError {
const error = new HttpErrors['422'](msg);
error.name = name ?? this.name;
return error;
}
Catch error examples here are for defaultSequence, overriding the handle method.
But nowdays app template uses MiddlewareSequence.
So here is the example, how tomodify the response in middleware sequence, you can use this example:
import { Middleware, MiddlewareContext } from '#loopback/rest';
export const ErrorMiddleware: Middleware = async (middlewareCtx: MiddlewareContext, next) => {
// const {response} = middlewareCtx;
try {
// Proceed with next middleware
return await next();
} catch (err) {
// Catch errors from downstream middleware
// How to catch specific error and how to send custom error response:
if (HttpErrors.isHttpError(err) || (err as HttpErrors.HttpError).statusCode) {
const code: string = (err.statusCode || 500).toString();
if (code.length && code[0] === '4') {
response.status(Number(code) || 500);
return response.send({
error: {
message: err.message,
name: err.name || 'UnknownError',
statusCode: code
}
});
}
}
throw err;
}
};
And register the middleware in application.ts
this.middleware(ErrorMiddleware);

Unable to get error message from API Angular 6

I use the following function to Post a object of a given class.
public Post<T>(object: T, url: string, httpOptions: {}): Observable<T> {
return this.httpClient.post<T>(`${environment.apiEndpoint}` + url, object, httpOptions)
.pipe(
catchError(this.handleError)
);
}
This function is called in all the service that wants to post something. Like this.
public addEquipment(equipment: Equipment): Observable<Equipment> {
return this.shared.Post<Equipment>(equipment, this.url, this.header);
}
addEquipment is then executed within the component that uses that service. Like this.
this.equipmentService.addEquipment(result)
.subscribe((data: any) => { this.alertService.success(data) }, (error: any) => this.alertService.error(error));
The problem is when the API returns a error (that I can see includes a error message, in the network tab) it tells me that there is no body in the response. The API returns a HttpResult where the error message is added to the response field.
return new HttpResult { StatusCode = HttpStatusCode.Conflict, Response = "Error message"}
I use the following function to handle the errors.
private handleError(error: HttpErrorResponse) {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
console.error('An error occurred:', error.error.message);
}
else {
console.log(error);
console.error(
`Backend returned code ${error.status}, ` +
`body was: ${error.error}`);
}
console.log(error);
return throwError(
error.error)
};
It is Angular 6 and a ServiceStack API.
All suggestions would be appreciated.
FYI it's preferable to return structured error responses in ServiceStack which you can do with:
HttpError.Conflict("Error message");
Which will let you catch it when using ServiceStack's TypeScript ServiceClient with:
try {
var response = await client.post(request);
} catch (e) {
console.log(e.responseStatus.message);
}
But from this answer for handling errors with Angular HTTP Client it suggests the error body should be accessible with:
this.httpClient
.get("data-url")
.catch((err: HttpErrorResponse) => {
// simple logging, but you can do a lot more, see below
console.error('An error occurred:', err.error);
});

createUserWithEmailAndPassword redirect causes an error

I want to redirect a user to a part of my site after signup, not signin
So at the end of the createUserWithEmailAndPassword function I change the window.location and it all works fine, I do get an error since the function was interrupted
How do I avoid this? There's signInWithRedirect and getRedirectResult but the Firebase documentation doesn't include email as a provider, only facebook
I can try-catch the error but perhaps there's a more elegant way to tackle this?
function handleSignUp() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});
window.location = "/add-location";
}
// console error
A network error (such as timeout, interrupted connection or unreachable host) has occured.
Simply redirect after successful sign-up:
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(user) {
window.location = "/add-location";
})
.catch(function(error) {
...
});
You were redirecting before the promise resolved, hence the interrupted operation error.
What about redirecting after login callback?
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in. So redirect:
window.location = "/add-location";
} else {
// No user is signed in.
}
});
As mentioned in the docs, after creation, auth status changes to logged in automatically so that will trigger the redirection after getting a successful login.