Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3dc7d2b

Browse files
author
Cristian
committed
Account page (WIP). Needs some refactor
1 parent f28363d commit 3dc7d2b

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

‎imports/api/security.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ export default class Security {
22
static isAdmin() {
33
return Roles.userIsInRole(Meteor.userId(), ['admin'], 'default-group');
44
}
5+
6+
static checkLoggedIn() {
7+
if (!Meteor.userId()) {
8+
throw new Meteor.Error('not-authorized', 'You are not logged in');
9+
}
10+
}
511
}

‎imports/api/users/server/methods.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import Security from '../../security';
2+
import RepoUsers from '../../../db/users/repository';
3+
14
Meteor.methods({
25
getEmailFromToken(token) {
36
/* Get user email */
@@ -20,4 +23,8 @@ Meteor.methods({
2023
Roles.addUsersToRoles(id, 'user', 'default-group');
2124
return true;
2225
},
26+
getUserData() {
27+
Security.checkLoggedIn();
28+
return RepoUsers.findOne(Meteor.userId());
29+
},
2330
});

‎imports/startup/client/routes.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import HomePage from '../../ui/pages/HomePage';
1111
import LoginPage from '../../ui/pages/LoginPage';
1212
import RegisterPage from '../../ui/pages/RegisterPage';
1313
import ForgotPasswordPage from '../../ui/pages/ForgotPasswordPage';
14+
import MyAccountPage from '../../ui/pages/MyAccountPage';
1415

1516
const history = createHistory();
1617

@@ -43,6 +44,11 @@ const routes = (
4344
path="/forgot-password"
4445
render={() => (Meteor.userId() ? <Redirect to="/" /> : <ForgotPasswordPage />)}
4546
/>
47+
<Route
48+
exact
49+
path="/my-account"
50+
render={() => (Meteor.userId() ? <MyAccountPage /> : <Redirect to="/login" />)}
51+
/>
4652
</Switch>
4753
</MainLayout>
4854
</Router>

‎imports/ui/pages/MyAccountPage.js‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import React, { Component } from 'react';
2+
import PropTypes from 'prop-types';
3+
import {
4+
Button, Form, Container, Header, Segment, Message, Icon, Popup,
5+
} from 'semantic-ui-react';
6+
7+
class MyAccountPage extends Component {
8+
state = {
9+
username: '',
10+
oldpassword: '',
11+
newpassword: '',
12+
error: '',
13+
};
14+
15+
componentDidMount() {
16+
Meteor.call('getUserData', (err, res) => {
17+
if (err) {
18+
Bert.alert('There was an error trying to get your user data');
19+
return false;
20+
}
21+
22+
this.setState({ username: res.username });
23+
});
24+
}
25+
26+
handleChange = (e, { name, value }) => this.setState({ [name]: value });
27+
28+
handleSubmit = () => {
29+
const { email, password } = this.state;
30+
const { history } = this.props;
31+
32+
if (!this.validForm()) return false;
33+
Meteor.loginWithPassword(email, password, (err) => {
34+
if (err) {
35+
this.setState({ error: err.reason });
36+
return false;
37+
}
38+
Bert.alert('Logged in', 'success');
39+
history.push('/');
40+
});
41+
};
42+
43+
validForm = () => {
44+
const { username } = this.state;
45+
46+
if (!username) {
47+
this.setState({ error: 'There are empty required fields' });
48+
return false;
49+
}
50+
return true;
51+
};
52+
53+
render() {
54+
const {
55+
username, error, oldpassword, newpassword,
56+
} = this.state;
57+
return (
58+
<Container>
59+
<Header as="h1">My Account</Header>
60+
<Segment padded>
61+
<Message hidden={!error} color="red">
62+
{error}
63+
</Message>
64+
<Form>
65+
<Form.Input
66+
onChange={this.handleChange}
67+
value={username}
68+
name="username"
69+
fluid
70+
required
71+
label="Username"
72+
type="text"
73+
placeholder="Username"
74+
/>
75+
<Button fluid color="black" onClick={this.handleSubmit} type="submit">
76+
Save
77+
</Button>
78+
</Form>
79+
<Form>
80+
<Form.Input
81+
onChange={this.handleChange}
82+
value={oldpassword}
83+
name="oldpassword"
84+
fluid
85+
required
86+
label="Old password"
87+
type="password"
88+
placeholder="Old password"
89+
/>
90+
<Form.Input
91+
onChange={this.handleChange}
92+
value={newpassword}
93+
name="newpassword"
94+
fluid
95+
required
96+
label="New password"
97+
type="password"
98+
placeholder="New password"
99+
/>
100+
<Button fluid color="black" onClick={this.changePassword} type="submit">
101+
Change password
102+
</Button>
103+
</Form>
104+
</Segment>
105+
</Container>
106+
);
107+
}
108+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /