Fakultas Ilmu Komputer UI

Commit f0afd1b4 authored by Zamil Majdy's avatar Zamil Majdy
Browse files

[#140654507] #13 Add login redirection

parent f86c1bbb
...@@ -35,11 +35,11 @@ export default class App extends React.Component { ...@@ -35,11 +35,11 @@ export default class App extends React.Component {
<Route path="/register" component={CompanyRegister} /> <Route path="/register" component={CompanyRegister} />
<Route component={Dashboard} onEnter={this.handleAuth}> <Route component={Dashboard} onEnter={this.handleAuth}>
<Route path="/" component={Profile} /> <Route path="/" component={Profile} />
<Route path="profile" component={Profile} /> <Route path="/profile" component={Profile} />
<Route path="lowongan" component={VacancyPage} /> <Route path="/lowongan" component={VacancyPage} />
<Route path="users" component={Profile} /> <Route path="/users" component={Profile} />
</Route> </Route>
<Redirect from="*" to="/login" /> <Redirect from="*" to={Server.isLoggedIn() ? '/lowongan' : '/login'} />
</Router> </Router>
); );
} }
......
...@@ -2,6 +2,7 @@ import React from 'react'; ...@@ -2,6 +2,7 @@ import React from 'react';
import { Form, Input, Button, Message, Image } from 'semantic-ui-react'; import { Form, Input, Button, Message, Image } from 'semantic-ui-react';
import { browserHistory } from 'react-router'; import { browserHistory } from 'react-router';
import Server from '../lib/Server'; import Server from '../lib/Server';
import Storage from '../lib/Storage';
export default class LoginForm extends React.Component { export default class LoginForm extends React.Component {
...@@ -21,7 +22,7 @@ export default class LoginForm extends React.Component { ...@@ -21,7 +22,7 @@ export default class LoginForm extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
/* istanbul ignore next */ /* istanbul ignore next */
this.state = { email: '', password: '', errorFlag: false }; this.state = { username: '', password: '', errorFlag: false };
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
} }
...@@ -33,11 +34,13 @@ export default class LoginForm extends React.Component { ...@@ -33,11 +34,13 @@ export default class LoginForm extends React.Component {
handleSubmit(event) { handleSubmit(event) {
event.preventDefault(); event.preventDefault();
const data = { const data = {
type: this.props.type, 'login-type': this.props.type,
email: this.state.email, username: this.state.username,
password: this.state.password, password: this.state.password,
}; };
Server.post('api/login/', data).then(() => {
Server.post('/login/', data).then((response) => {
Storage.set('user-data', response);
browserHistory.push('/home'); browserHistory.push('/home');
}, () => { }, () => {
this.setState({ errorFlag: true }); this.setState({ errorFlag: true });
...@@ -52,8 +55,8 @@ export default class LoginForm extends React.Component { ...@@ -52,8 +55,8 @@ export default class LoginForm extends React.Component {
</div> </div>
<Form.Group widths="equal"> <Form.Group widths="equal">
<Form.Field required> <Form.Field required>
<label htmlFor="id"> Email </label> <label htmlFor="id"> Username </label>
<Input type="text" id="email" icon="user" iconPosition="left" placeholder="email" onChange={e => this.handleChange(e, 'email')} required /> <Input type="text" id="username" icon="user" iconPosition="left" placeholder="username" onChange={e => this.handleChange(e, 'username')} required />
</Form.Field> </Form.Field>
</Form.Group> </Form.Group>
...@@ -67,7 +70,7 @@ export default class LoginForm extends React.Component { ...@@ -67,7 +70,7 @@ export default class LoginForm extends React.Component {
<Button type="submit" fluid color="blue">Login</Button> <Button type="submit" fluid color="blue">Login</Button>
<Message <Message
error error
content="Login gagal: email atau password salah." content="Login gagal: username atau password salah."
/> />
</Form> </Form>
......
import React from 'react'; import React from 'react';
import { Menu, Image } from 'semantic-ui-react'; import { Menu, Image } from 'semantic-ui-react';
import { Link } from 'react-router'; import { Link, browserHistory } from 'react-router';
import Server from '../lib/Server'; import Server from '../lib/Server';
import Storage from '../lib/Storage';
export default class TopMenu extends React.Component { export default class TopMenu extends React.Component {
state = { activeItem: 'home' }; constructor(props) {
super(props);
/* istanbul ignore next */
this.logout = this.logout.bind(this);
}
handleItemClick = (e, { name2 }) => this.setState({ activeItem: name }) logout = () => {
Server.get('/api-auth/logout/?next=/', true).then(() => {
Storage.clear();
browserHistory.push('/login');
});
};
render() { render() {
const { activeItem } = this.state
return ( return (
<Menu pointing secondary> <Menu pointing secondary>
<Image as="a" size="small" src="/assets/img/logo.png" href="/" /> <Image as="a" size="small" src="/assets/img/logo.png" href="/" />
...@@ -17,9 +27,9 @@ export default class TopMenu extends React.Component { ...@@ -17,9 +27,9 @@ export default class TopMenu extends React.Component {
<Menu.Item as={Link} to="/lowongan" name="home" /> <Menu.Item as={Link} to="/lowongan" name="home" />
<Menu.Item as={Link} to="/profile" name="profil" /> <Menu.Item as={Link} to="/profile" name="profil" />
{Server.isLoggedIn() ? {Server.isLoggedIn() ?
<Menu.Item as={Link} href="/api/api-auth/logout/?next=/" name="logout" /> : <Menu.Item as={Link} onClick={this.logout} name="logout" /> :
<Menu.Item as={Link} to="/login" name="login" /> <Menu.Item as={Link} to="/login" name="login" />
} }
</Menu.Menu> </Menu.Menu>
</Menu> </Menu>
); );
......
...@@ -44,7 +44,7 @@ export default class Server { ...@@ -44,7 +44,7 @@ export default class Server {
if (response.status === 204) { if (response.status === 204) {
return response; return response;
} }
return response.json(); return response;
}); });
/* istanbul ignore next */ /* istanbul ignore next */
......
...@@ -31,6 +31,7 @@ class UserViewSet(viewsets.ModelViewSet): ...@@ -31,6 +31,7 @@ class UserViewSet(viewsets.ModelViewSet):
return [AllowAny()] return [AllowAny()]
return super(UserViewSet, self).get_permissions() return super(UserViewSet, self).get_permissions()
class StudentViewSet(viewsets.ModelViewSet): class StudentViewSet(viewsets.ModelViewSet):
queryset = Student.objects.all() queryset = Student.objects.all()
serializer_class = StudentSerializer serializer_class = StudentSerializer
......
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.generics import get_object_or_404 from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response from rest_framework.response import Response
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany
...@@ -13,6 +14,11 @@ class VacancyViewSet(viewsets.ModelViewSet): ...@@ -13,6 +14,11 @@ class VacancyViewSet(viewsets.ModelViewSet):
serializer_class = VacancySerializer serializer_class = VacancySerializer
permission_classes = [IsAdminOrCompany] permission_classes = [IsAdminOrCompany]
def get_permissions(self):
if self.action in ["get", "list"]:
return [IsAuthenticated()]
return super(VacancyViewSet, self).get_permissions()
class ApplicationViewSet(viewsets.GenericViewSet): class ApplicationViewSet(viewsets.GenericViewSet):
serializer_class = VacancySerializer serializer_class = VacancySerializer
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment