create account

Restricting auth required actions for guest users on HapRamp Alpha Web App by singhpratyush

View this thread on: hive.blogpeakd.comecency.com
· @singhpratyush · (edited)
$0.30
Restricting auth required actions for guest users on HapRamp Alpha Web App
#### Repository

https://github.com/hapramp/alpha-web

### Bug Description

The HapRamp alpha web app allows users to browse the app as a guest (i.e. without signing in). This is a good thing for people who just come in to read some content and walk away. But it may be possible that these guest users try to rate/comment on the app.

Previously, there was no check for auth status and hence the actions were not restricted. In this post, I will be describing the way in which I handled this case without having to change a significant part of the code.

### Solution

To achieve this, I created an intermediate wrapper for the actions which would check the login status of current user and dispatch accordingly. It worked using the following logic - 

- Is the user logged in while performing the action?
  - Yes
    Let him perform the action
  - No
    Toast a UI notification asking them to log in.

The function looks something like this - 

```js
import notify from './notification';  // For creating UI notifications
import getStore from './storeUtils';  // For getting store state

const notAuthorizedError = () => dispatch => {
	notify.danger('Please log in first!');
	return Promise.reject();
};

export const authRequired = target => getStore().getState().login.isLoggedIn ? target : notAuthorizedError;
```

All the actions that required auth from user were then wrapped with this function to achieve the desired results. Here is an example - 

```js
export const ratePost = authRequired((author, permlink, vote) => dispatch => {
  ...  // Implementation of actual action
});
```

##### Commits
- https://github.com/hapramp/alpha-web/commit/55c43f973ed6b48126d298fcd5470aef880ea57d
- https://github.com/hapramp/alpha-web/commit/83c08b2f09954e7f665305b8dac4297df479f579
- https://github.com/hapramp/alpha-web/commit/72e2ee624fa55e4e0d613c2d0c9b3aed8b4b7073

**PS**: [We just went open source](https://busy.org/@singhpratyush/open-sourcing-hapramp-alpha-web-app-and-adding-travis-ci) and are looking for helping hands. If you are interested, please visit https://github.com/hapramp/alpha-web.

#### Github Account
https://github.com/singhpratyush
👍  , , , , , , , , , , , ,
properties (23)
authorsinghpratyush
permlinkrestricting-auth-required-actions-for-guest-users-on-hapramp-alpha-web-app
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.4.0","format":"markdown","tags":["utopian-io","development","hapramp","technology","steem"],"links":["https://github.com/hapramp/alpha-web","https://github.com/hapramp/alpha-web/commit/55c43f973ed6b48126d298fcd5470aef880ea57d","https://github.com/hapramp/alpha-web/commit/83c08b2f09954e7f665305b8dac4297df479f579","https://github.com/hapramp/alpha-web/commit/72e2ee624fa55e4e0d613c2d0c9b3aed8b4b7073","https://busy.org/@singhpratyush/open-sourcing-hapramp-alpha-web-app-and-adding-travis-ci","https://github.com/hapramp/alpha-web","https://github.com/singhpratyush"],"users":["singhpratyush"]}
created2018-06-06 08:16:39
last_update2018-06-06 08:38:18
depth0
children2
last_payout2018-06-13 08:16:39
cashout_time1969-12-31 23:59:59
total_payout_value0.238 HBD
curator_payout_value0.057 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,156
author_reputation7,035,648,262,478
root_title"Restricting auth required actions for guest users on HapRamp Alpha Web App"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id59,495,045
net_rshares101,222,739,152
author_curate_reward""
vote details (13)
@amosbastian ·
Hi @singhpratyush, thanks for the contribution. It would be best to include more work per contribution as that will exponentially increase your chances of also getting rewarded for it. Also, please keep the following guideline in mind for future contributions

* Bug Fixes for contributor’s Own Projects will not be considered for potential reward, unless the Bugs were caused by third party dependencies.

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
properties (22)
authoramosbastian
permlinkre-singhpratyush-restricting-auth-required-actions-for-guest-users-on-hapramp-alpha-web-app-20180607t211032514z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["singhpratyush"],"links":["https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-07 21:10:33
last_update2018-06-07 21:10:33
depth1
children1
last_payout2018-06-14 21:10:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length575
author_reputation174,473,586,900,705
root_title"Restricting auth required actions for guest users on HapRamp Alpha Web App"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id59,746,602
net_rshares0
@singhpratyush ·
Thanks for your time @amosbastian. I'll keep that in mind.
properties (22)
authorsinghpratyush
permlinkre-amosbastian-re-singhpratyush-restricting-auth-required-actions-for-guest-users-on-hapramp-alpha-web-app-20180608t082751115z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"busy","app":"busy/2.4.0"}
created2018-06-08 08:27:51
last_update2018-06-08 08:27:51
depth2
children0
last_payout2018-06-15 08:27:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length58
author_reputation7,035,648,262,478
root_title"Restricting auth required actions for guest users on HapRamp Alpha Web App"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id59,816,093
net_rshares0