I'm currently working on a portal website with nodejs, express and angular. One issue I have come across is how error handling should be done. I have read through the express error handling guide also and have implemented their default error handling function. So, I can't seem to find the best practice for this situation.
For example, if I am trying to retrieve from a database and it cannot be found, the server would send a 404 and in https request would handle it there on the client end. The only problem is, is that it sends the error message and stack trace into the client's console. My method was, based on that error, I would handle it on the client end by displaying a message such as "Incorrect password" or "Cannot find this e-mail in the database. Would you like to sign up?".
Another example I have is when a user inputs his credit card information. On the client end, there is a http request for his billing information. If I receive a "404" for no billing information found, I would bring up a add billing form. If it is a "200", I would display that current billing information on that same page.
What would be the best practice to catching errors on the front end without sending errors to the user?
function getBillingInfo(){
$http.get('test/api/billinginfo', {
params: {
token: store.get('token')
}
}).then(function (response) {
//Retrieve billing information
$scope.billingInfo = response.data;
//Display current billing information
$scope.billingState = "display";
}, function (response) {
//Display add billing form
$scope.billingState = "add";
//Initialize form input
$scope.billingAdd = {
country:"US",
month:"month",
year:"year",
state:"AL"
};
});
}
Related
I am working with angular,Mongodb,Nodejs,express. simply a Mean stack.
my website is related to shopping where the carts loads dynamically. the products added to cart should be sent as mail to owner.
can we send directly mail? is yes please explain the process how.
if not should be stored in database then how?
So, the question is very unclear here. But I'll try my best to explain all the situations. So that, you don't have to mess with it a lot. I'll explain how you can send a mail to the user using front-end only or using your server.
To send an email to a user, we need to do it on the backend. That means you'll have to write some code in your server for sending the email to your client. As you have a MEAN Stack application, you must be familiar with Node package manager.
npm has a package called nodemailer that can be used for sending emails to the users. But make sure you write those emails in a good manner so that they don't end up in the spam box.
You can install nodemailer using
npm install nodemailer
Here's a sample code from w3schools
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail#gmail.com',
pass: 'yourpassword'
}
});
var mailOptions = {
from: 'youremail#gmail.com',
to: 'myfriend#yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
You may also find other and maybe even better alternatives with a simple google search.
Also, i don't think there's any point in storing this data in the database unless you need it for future use. I don't know the flow of your ecommerce application. So, I'll leave this decision for you to take.
And if you want to send the e-mail directly from the front-end, there's a workaround. Sending emails requires a server, but you can use someone's else backend server for this. Basically a BaaS(Backend as a Service). You can check this previously answered question on stackoverflow for more information regarding sending emails directly from the frontend of an application.
How can i send emails without a server ? Only front-end Javascript
I'm using react native. I want to edit a user's information by a user id. so wordpress provides the api. when i'm using, getting error 401.
Request failed with status code 401
docs: https://developer.wordpress.org/rest-api/reference/users/#update-a-user
const userDate = {
first_name: userDataAccount.firstName,
last_name: userDataAccount.lastName,
};
const user = await WordpressApi.post("/wp-json/wp/v2/users/" + userFullInfo.id, userDate);
why is that? Please help me...
i have fixed my problem. i added auth header in proper way even it didn't connected, getting error 401. but put http instead of https, it connects well. So that's ssl related problem, i need to double check wp ssl connection. Thank you
I call a dahua API for preset response in angular js but it gives me a 401 Unauthorised error.
My code is below :
var streamurl='http://admin:123456#192.168.1.202/cgi-bin/ptz.cgi?action=start&channel=0&code=PositionABS&arg1=180&arg2=190&arg3=10';
$http.get(streamurl, { withCredentials: true })
.then(function(response2) {
console.log(response2.data);
});
I could be wrong but I think you are getting login screen with that request. I've read through documentation and I have found your way of authentication only working on rtsp:// protocol, and for http I believe you should modify headers, and encode your username/pasword to base64, here's documentation:
ftp://ftp.wintel.fi/drivers/dahua/SDK-HTTP_ohjelmointi/DAHUA_IPC_HTTP_API_V1.00x.pdf
Also you should probably use this since you are using AngularJS/NodeJS, it will make your life easier - or check how this guy did his authentication and "borrow" from him:
https://github.com/nayrnet/node-dahua-api
How to Fix the 401 Unauthorized Error
Check for errors in the URL. It's possible that the 401 Unauthorized error appeared because the URL was typed incorrectly or the link that was clicked on points to the wrong URL - one that is for authorized users only.
If you're sure the URL is valid, visit the website's main page and look for a link that says Login or Secure Access. Enter your credentials here and then try the page again. If you don't have credentials, follow the instructions provided on the website for setting up an account.
If you're sure the page you're trying to reach shouldn't need authorization, the 401 Unauthorized error message may be a mistake. At that point, it's probably best to contact the webmaster or other website contact and inform them of the problem.
The 401 Unauthorized error can also appear immediately after login, which is an indication that the website received your username and password but found something about them to be invalid (e.g. your password is incorrect). Follow whatever process is in place at the website to regain access to their system.
From https://www.lifewire.com/401-unauthorized-error-what-it-is-and-how-to-fix-it-2622934
I am creating custom skill in Alexa which uses account linking. I have created my own authentication server using OAuth2 php library and I have configured the authorization url and token urls in skill configuration.
When I try account linking from Alexa mobile app, I get error 'unable to link your skill'. following is my work progress.
Alexa app is able to open my authentication url.
I am able authorize and provide authorization code with redirect uri.
Alexa is requesting for access token using authorization code previously provided.
I am able validate authorization code and response back with access token and refresh token.
Alexa fails in this step to link with my skill. It say's 'Unable to link your skill'.
I have gone through my forums about the same, but couldn't find what exactly the issue is. Could any one please help me out in this regard.
I was facing the same issue too , the problem was solved by selecting "credentials in request body" (default being Http basic) for "Client Authentication Scheme", since the access token in my case was sent in the body of the message. Check how the authentication token is sent by your server.
If your redirect link is currently:
https://layla.amazon.com/api/skill/link/xxxxxxxxxxxxxx?code=xxxxxxxxx&state=xxxxx
You need to change the ? to a #
e.g.
https://layla.amazon.com/api/skill/link/xxxxxxxxxxxxxx#code=xxxxxxxxx&state=xxxxx
Thought this might help anyone wondering how the Alexa service is posting to their OAuth endpoint since it's pretty opaque and undocumented. The redirect to the Alexa service initiates a POST request to the defined OAuth endpoint with the post body in x-www-form-urlencoded format not JSON. So the POST looks like this.
POST /authentication/1/oauth HTTP/1.1 url.Values{} grant_type=authorization_code&code=XXXXXXXXXXXXXXXXXXXXXXXXX&redirect_uri=https%253A%252F%252Fpitangui.amazon.com%252Fapi%252Fskill%252Flink%252FM9BEOG3DM65SQ&client_id=XXXXXXXXXXXXXXXXXXXXXX
If your endpoint isn't parsing this data or expecting some format that can be unmarshaled easily then it is probably failing with a 406 response.
In my case the problem was with the Client secret,
In google developer console add your skill redirect URIs
and recheck the client secret you provide in the alexa skill Authorization grant type
My issue was with the final AccessToken call. I was assuming it was using a GET request, so I only catered for this in my function. It is actually creating an access token. So it's using a POST.
After I updated my function to use a post and return the AccessToken in JSON format it all works fine.
Maybe the following steps will help you identify the problem:
Add console.log('LOGS', response) to your Lambda function.
Activate the skill and login in the Alexa app
Go back to your Lambda function and check the last logs for the LOGS entry.
If you find that the Lambda function is invoked than the problem is not from your OAuth server, but you may need to handle the "AcceptGrant directive" in your Lambda function as it is motioned here: https://developer.amazon.com/en-US/docs/alexa/device-apis/alexa-authorization.html#directives
adjust your Lambda function to:
exports.handler = function (request, context) {
if (request.directive.header.namespace === 'Alexa.Authorization' && request.directive.header.name === 'AcceptGrant') {
log("DEBUG:", "Authorization request", JSON.stringify(request));
handleAcceptGrant(request, context);
}
function handleAcceptGrant(request, context) {
var response = {
event: {
header: {
"namespace": "Alexa.Authorization",
"name": "AcceptGrant.Response",
"messageId": request.directive.header.messageId,
"payloadVersion": "3"
},
payload: {}
}
};
log("DEBUG", "Alexa.Authorization ", JSON.stringify(response));
context.succeed(response);
}
If the problem is with the AcceptGrant then The account linking should be now successful.
I'm having a particular problem with my Service Worker in conjunction with my Google App Engine app. The service worker installs and activates perfectly. Also, I used the exact same service worker in another app that I built (which didn't require any user login) and it worked fine. However, on my current app, there is only one page (App Engine renders the page and sends it on successful login using the built-in user API). My problem is that I don't want the root address, a rendered page from the server, to be cached. Here is my files to cache array:
var filesToCache = ['/manifest.json','/js/app.js','/js/jquery-3.js','/js/jquery.matchHeight.js','/js/materialize2.min.js','/js/vue.js','/js/vuex.min.js','/js/chart.js','/css/materialize.min.css','/css/style.css', '/images/case.jpg'];
As you can see, I'm only trying to cache the scripts, since the HTML must be rendered server-side, and it is extremely compact, so I don't mind the small network request to be made. And here is my service worker which resides in the root folder.
self.addEventListener('install', function(e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName)
.then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
.catch(function(e) {
console.error(e);
})
);
});
self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', function(e) {
console.log('[ServiceWorker] Fetch')
e.respondWith(
caches.match(e.request)
.then(function(response) {
return response || fetch(e.request)
})
);
});
The problem is, despite the fact that I don't try to cache the root address, the service worker will try to return a response instead of a fetch for a user's initial try. This is the sequence of events after a user navigates to the app:
1 - Landing page with button showing 'Sign into Google'.
2 - Redirect to app upon successful login
Expected Result: user is redirected to Google's own solution, after which, the user will be redirected back into my app fully logged in.
Actual Result: service worker intercepts request, serves undefined response, redirects user back to login page ad infinitum.
I'm pretty new to service workers, so perhaps there is something in missing in intercepting response and some edge case I need to account for, but I'm not sure what it is. I've tried splitting up the return statement to first check if there is a response, so if the response is undefined, it skips the return response statement, cloning the event, and then returning the event request in a fetch request. However, I got exactly the same result.
Has anybody come across this before or know what to do? I spent all yesterday on this to no avail and just gave up on returning any response at all, serving everything from the network, but I really want to solve it. Thanks for any help in advance.