keystone.js Currently logged in user - node.js

I'm new to Keystone, but have been trying all day to find the currently logged in user name but I'm not clear how to do this.
If I take the index view from keystone for example
{{!< default}}
<div class="container">
<div class="jumbotron"><img src="/images/logo.svg" width="160">
<h1>Welcome</h1>
<p>This is your new <a href='http://keystonejs.com' target='_blank'>KeystoneJS</a> website.</p>
<p>
It includes the latest versions of
<a href='http://getbootstrap.com/' target='_blank'>Bootstrap</a>
and <a href='http://www.jquery.com/' target='_blank'>jQuery</a>.
</p>
<p>Visit the <a href='http://keystonejs.com/guide' target='_blank'>Getting Started</a> guide to learn how to customise it.</p>
<hr>
<p>We have created a default Admin user for you with the email <strong>masterofimps#yahoo.co.uk</strong> and the password <strong>admin</strong>.</p>
<p>Sign in to use the Admin UI.</p>
<hr>
<p>
Remember to <a href='https://github.com/keystonejs/keystone' target='_blank'>Star KeystoneJS on GitHub</a> and
<a href='https://twitter.com/keystonejs' target='_blank'>follow #keystonejs</a> on twitter for updates.
</p>
</div>
</div>
and I want to add the user name to the view code somthing like
<h1>Welcome Tim</h1>
I have tried
<h1>Welcome {{locals.user}}</h1>
<h1>Welcome {{locals.user.name}}</h1>
<h1>Welcome {{req.user}}</h1> (using an Express request)
But to no avail. How do I find the user name from the User model? Do I have to define something else in the ..\routes\views first?
If anyone could help with an example or pointer in the right direction, Id be very grateful indeed!

If you used the Yeoman generator, one of the middleware functions included (initLocals) sets the current user to a local variable.
res.locals.user = req.user;
https://github.com/keystonejs/generator-keystone/blob/master/app/templates/routes/_middleware.js#L27
You don't need to include locals before the variable name in Handlebars. So remove it in order to get the current user information.
{{user}}
{{user.name}}

Related

express-flash not showing message until refresh

I have express-flash installed and working, but only after I refresh the page I was redirected to. My controllers are using flash like so
req.flash("info", "No changes were made.");
res.redirect("/admin/dashboard");
In the view, I am importing a partial that looks like this.
<% if(messages.info) { %>
<div class="modal-content flashModal" id="message-info">
<div class="header">
<span id="closeBtn">×</span>
</div>
<div class="modal-body">
<strong>
<%= messages.info %>
</strong>
</div>
</div>
<% } %>
Not exactly sure what is causing the message to appear only after I have refreshed. It looks like the redirect is happening before the flash message? The documentation seemed straight forward enough but doesn't cover an issue like this. Any help is appreciated.
https://www.npmjs.com/package/express-flash
Try to use render instead of redirect. See if it works.
res.render('dashboard);

Nodejs writing a scraper that can read JS protected websites

nodejs is new for me, and I've just started learning web scrapers. The problem is that I need to scrap a website that is protected with JS. So I need to get phones from the website, but div with the phone number appears only after user clicks on "show number" button. Are there any ways or npm to get the numbers? Here is the website enter link description here, but it is in russian language. So the button is "показать телефоны". Thank you in advance!
Go to the page with the phone numbers and open the console and look at your network tools. When you click on a phone number it makes an ajax request and gives you a nice formatted response
{"status":"ok","phone":"+7 (727) 317-20-86","html_tooltip":"<section class=\"company-phones-tooltip\">\r\n <div class=\"company-phones-tooltip__wrap\">\r\n <header class=\"company-phones-tooltip__header\">\r\n Inform the manager that you learned the information on Allbiz.\r\n <\/header>\r\n <ul class=\"company-phones-tooltip-list\">\r\n <li class=\"company-phones-tooltip-list__item\">\r\n <div class=\"company-phones-tooltip-list__name\">\r\n management\r\n <\/div>\r\n <div class=\"company-phones-tooltip-list__number\">\r\n +7 (727) 317-20-86\r\n <\/div>\r\n <\/li>\r\n <\/ul>\r\n <footer class=\"company-phones-tooltip__footer\">\r\n <a class=\"company-phones-tooltip__link\" href=\"https:\/\/12246-kz.all.biz\/contacts\" target=\"_blank\">\r\n Show all contacts\r\n <\/a>\r\n <\/footer>\r\n <\/div>\r\n<\/section>"}
In order to emulate this you just need to make a call to the http://api.all.biz/ajax/viewphonenew/kz endpoint with the correct parameters after scraping the page. And all the query params that are needed for this endpoint is in the html element.
<div class="company-phones__wrap" data-click="company-phones" data-entid="58474" data-verify="bYjmFpAfm5QWOgIjx8cyNOARdSG3FIoPo6he2dYGLIc=" data-phone="Zk6xDyCXPMqWMXgTaCI51A24FHIsDwuy8IaF993LsHI=" data-country="kz" data-placement="company-phones-tooltip___3" data-tooltip-direction="left" data-source="list">
<div class="company-phones__code">+7(7 </div>
<div class="company-phones__main" data-ga="show-phones-list" data-ga-id="">
<div class="company-phones__btn">Показать телефоны</div>
</div>
</div>

show element on specific user with nodejs

I'm building a social app and under each image I have a comment section. I want to render every comment and to include a delete button only to the user that made the comment. Can anyone help me handle this?
here is how I render the comments:
{{# each image.comments}}
<div class="comments clearfix">
<img src="{{this.avatar}}" class="logo">
<span><strong>{{this.userName}}</strong><br> {{this.comment}}</span>
</div>
{{/each}}
`
You can track the comments for the images in the database. When a user comments for an image store that comment as well as check if the user Id is same as the owner id of the image. If so, then the user who just comments must be the owner and set a property like "isOwner = true" for the comment, along with the comment text.
And in the front end you can check this property and set a delete icon..
{{#each image.comments}}
<div class="comments clearfix">
<img src="{{this.avatar}}" class="logo">
<span><strong>{{this.userName}}</strong><br> {{this.comment}}</span>
{{#if this.isOwner}}
<i class="fa fa-trash"></i>
{{/if}}
</div>
{{/each}}

How to disable specific shipping method based on product's attribute value from checkout process

From, order_wizard_shipmethod_module.tpl
I found this,
{{#each shippingMethods}}
<a data-action="select-delivery-option-radio"
class="order-wizard-shipmethod-module-option {{#if isActive}}order-wizard-shipmethod-module-option-active{{/if}}"
data-value="{{internalid}}">
<input type="radio" name="delivery-options" data-action="edit-module" class="order-wizard-shipmethod-module-checkbox"
{{#if isActive}}checked{{/if}}
value="{{internalid}}"
id="delivery-options-{{internalid}}" />
<span class="order-wizard-shipmethod-module-option-name">{{name}}
<span class="order-wizard-shipmethod-module-option-price">{{rate_formatted}}</span>
</span>
</a>
{{/each}}
I'm wondering how can I get product's attribute value within shopping cart and disable specific shipping method?
If you're looking for the attribute values of each shipping method, then go to the Javascript file OrderWizard.Module.CartSummary.js and in the getContext function, after the confirmation variable is defined, put in console.log(summary);.
In your Cart, look in the console and within attributes is shipmethod and you'll find what you need in there.

How to check logged user in liferay velocity?

I have a requirement that I have to display sign in url in webcontent, if user not logged in and also need to display if user logged I have to display sign out hyper link. I have searched somewhere got $is_signed_in is for checking user logged in or not. After that I have added the below code to my requirement. Even though user is logged or not it is displaying "To know more please Sign In". Why condition failed? and how to check user logged in or not in liferay velocity? I have observed that I am not getting the values of $is_signed_in,$sign_in_url,$sign_out_url
#if(!$is_signed_in)
To know more please Sign In
#else
click here to Sign Out
#end
$is_signed_in is available in themes, not in webcontent templates.
Try
#set ($themeDisplay = $httpServletRequest.getAttribute("THEME_DISPLAY"))
$themeDisplay.isSignedIn()
this should work in webcontent templates.
(Thanks #Gonzalo for the reminder to initialize $themeDisplay prior to use)
in your theme _diff/templates/portal_normal.vm, you have add this code.
if ($is_signed_in)
<!-- user dropdown starts -->
<div class="btn-group pull-right" >
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<i class="icon-user"></i><span class="hidden-phone"> $user_name </span>
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>Logout</li>
</ul>
</div>
<!-- user dropdown ends -->
#end
Check the below link -
https://www.liferay.com/community/forums/-/message_boards/message/25175535
Try adding this code in portal-ext.properties
journal.template.velocity.restricted.variables=

Resources