Skip to content

Commit e948d81

Browse files
committed
Base attempt at using vuex to handle logins
1 parent cc58bc9 commit e948d81

File tree

12 files changed

+218
-202
lines changed

12 files changed

+218
-202
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Api\Client;
4+
5+
use Illuminate\Http\Request;
6+
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
7+
8+
class AccountController extends ClientApiController
9+
{
10+
public function index(Request $request): array
11+
{
12+
return $this->fractal->item($request->user())
13+
->transformWith($this->getTransformer(AccountTransformer::class))
14+
->toArray();
15+
}
16+
}

app/Http/Controllers/Auth/AbstractLoginController.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Http\Controllers\Auth;
44

5-
use Cake\Chronos\Chronos;
65
use Lcobucci\JWT\Builder;
76
use Illuminate\Http\Request;
87
use Pterodactyl\Models\User;
@@ -16,6 +15,7 @@
1615
use Illuminate\Contracts\Encryption\Encrypter;
1716
use Illuminate\Foundation\Auth\AuthenticatesUsers;
1817
use Pterodactyl\Traits\Helpers\ProvidesJWTServices;
18+
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
1919
use Illuminate\Contracts\Cache\Repository as CacheRepository;
2020
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2121

@@ -137,24 +137,18 @@ protected function sendLoginResponse(User $user, Request $request): JsonResponse
137137
$request->session()->regenerate();
138138
$this->clearLoginAttempts($request);
139139

140-
$token = $this->builder->setIssuer(config('app.url'))
141-
->setAudience(config('app.url'))
142-
->setId(str_random(12), true)
143-
->setIssuedAt(Chronos::now()->getTimestamp())
144-
->setNotBefore(Chronos::now()->getTimestamp())
145-
->setExpiration(Chronos::now()->addSeconds(config('session.lifetime'))->getTimestamp())
146-
->set('user', $user->only([
147-
'id', 'uuid', 'username', 'email', 'name_first', 'name_last', 'language', 'root_admin',
148-
]))
149-
->sign($this->getJWTSigner(), $this->getJWTSigningKey())
150-
->getToken();
151-
152140
$this->auth->guard()->login($user, true);
153141

142+
debug($request->cookies->all());
143+
154144
return response()->json([
155145
'complete' => true,
156146
'intended' => $this->redirectPath(),
157-
'token' => $token->__toString(),
147+
'cookie' => [
148+
'name' => config('session.cookie'),
149+
'value' => $this->encrypter->encrypt($request->cookie(config('session.cookie'))),
150+
],
151+
'user' => (new AccountTransformer())->transform($user),
158152
]);
159153
}
160154

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Pterodactyl\Transformers\Api\Client;
4+
5+
use Pterodactyl\Models\User;
6+
7+
class AccountTransformer extends BaseClientTransformer
8+
{
9+
/**
10+
* Return the resource name for the JSONAPI output.
11+
*
12+
* @return string
13+
*/
14+
public function getResourceName(): string
15+
{
16+
return 'user';
17+
}
18+
19+
/**
20+
* Return basic information about the currently logged in user.
21+
*
22+
* @param \Pterodactyl\Models\User $model
23+
* @return array
24+
*/
25+
public function transform(User $model)
26+
{
27+
return [
28+
'admin' => $model->root_admin,
29+
'username' => $model->username,
30+
'email' => $model->email,
31+
'first_name' => $model->name_first,
32+
'last_name' => $model->name_last,
33+
'language' => $model->language,
34+
];
35+
}
36+
}

resources/assets/scripts/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ import { Ziggy } from './helpers/ziggy';
88
import Locales from './../../../resources/lang/locales';
99
import { flash } from './mixins/flash';
1010
import { routes } from './routes';
11-
import { storeData } from './store';
11+
import storeData from './store/index.js';
1212

1313
window.events = new Vue;
1414
window.Ziggy = Ziggy;
1515

16+
Vue.config.productionTip = false;
1617
Vue.use(Vuex);
18+
1719
const store = new Vuex.Store(storeData);
1820
const route = require('./../../../vendor/tightenco/ziggy/src/js/route').default;
1921

20-
Vue.config.productionTip = false;
2122
Vue.mixin({ methods: { route } });
2223
Vue.mixin(flash);
2324

resources/assets/scripts/components/auth/LoginForm.vue

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,21 @@
7777
this.$data.showSpinner = true;
7878
7979
this.clearFlashes();
80-
axios.post(this.route('auth.login'), {
81-
user: this.$props.user.email,
82-
password: this.$props.user.password,
83-
})
84-
.then(function (response) {
85-
// If there is a 302 redirect or some other odd behavior (basically, response that isnt
86-
// in JSON format) throw an error and don't try to continue with the login.
87-
if (!(response.data instanceof Object)) {
88-
throw new Error('An error was encountered while processing this request.');
80+
this.$store.dispatch('auth/login', { user: this.$props.user.email, password: this.$props.user.password })
81+
.then(response => {
82+
if (response.complete) {
83+
return window.location = response.intended;
8984
}
9085
91-
if (response.data.complete) {
92-
localStorage.setItem('token', response.data.token);
93-
self.$store.dispatch('login');
94-
return window.location = response.data.intended;
95-
}
96-
97-
self.$props.user.password = '';
98-
self.$data.showSpinner = false;
99-
self.$router.push({name: 'checkpoint', query: {token: response.data.login_token}});
86+
this.$props.user.password = '';
87+
this.$data.showSpinner = false;
88+
this.$router.push({name: 'checkpoint', query: {token: response.login_token}});
10089
})
101-
.catch(function (err) {
102-
self.$props.user.password = '';
103-
self.$data.showSpinner = false;
104-
self.$refs.password.focus();
105-
self.$store.dispatch('logout');
90+
.catch(err => {
91+
this.$props.user.password = '';
92+
this.$data.showSpinner = false;
93+
this.$refs.password.focus();
94+
this.$store.dispatch('auth/logout');
10695
10796
if (!err.response) {
10897
return console.error(err);

resources/assets/scripts/components/dashboard/Dashboard.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<script>
3131
import { DateTime } from 'luxon';
32-
import { ServerCollection } from '../../models/server';
32+
import Server from '../../models/server';
3333
import _ from 'lodash';
3434
import Flash from '../Flash';
3535
import ServerBox from './ServerBox';
@@ -44,7 +44,7 @@
4444
documentVisible: true,
4545
loading: true,
4646
search: '',
47-
servers: new ServerCollection,
47+
servers: [],
4848
}
4949
},
5050
@@ -83,14 +83,14 @@
8383
this.clearFlashes();
8484
})
8585
.then(response => {
86-
this.servers = new ServerCollection;
86+
this.servers = [];
8787
response.data.data.forEach(obj => {
88-
this.getResourceUse(
89-
this.servers.add(obj.attributes)
90-
);
88+
const s = new Server(obj.attributes);
89+
this.servers.push(s);
90+
this.getResourceUse(s);
9191
});
9292
93-
if (this.servers.models.length === 0) {
93+
if (this.servers.length === 0) {
9494
this.info(this.$t('dashboard.index.no_matches'));
9595
}
9696
})
Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,3 @@
1-
import { Collection, Model } from 'vue-mc';
1+
export default class Server {
22

3-
/**
4-
* A generic server model used throughout the code base.
5-
*/
6-
export class Server extends Model {
7-
/**
8-
* Identifier the primary identifier for this model.
9-
*
10-
* @returns {{identifier: string}}
11-
*/
12-
static options() {
13-
return {
14-
identifier: 'identifier',
15-
};
16-
}
17-
18-
/**
19-
* Return the defaults for this model.
20-
*
21-
* @returns {object}
22-
*/
23-
static defaults() {
24-
return {
25-
uuid: null,
26-
identifier: null,
27-
name: '',
28-
description: '',
29-
node: '',
30-
limits: {
31-
memory: 0,
32-
swap: 0,
33-
disk: 0,
34-
io: 0,
35-
cpu: 0,
36-
},
37-
allocation: {
38-
ip: null,
39-
port: null,
40-
},
41-
feature_limits: {
42-
databases: 0,
43-
allocations: 0,
44-
},
45-
};
46-
}
47-
48-
/**
49-
* Mutations to apply to items in this model.
50-
*
51-
* @returns {{name: StringConstructor, description: StringConstructor}}
52-
*/
53-
static mutations() {
54-
return {
55-
uuid: String,
56-
identifier: String,
57-
name: String,
58-
description: String,
59-
node: String,
60-
limits: {
61-
memory: Number,
62-
swap: Number,
63-
disk: Number,
64-
io: Number,
65-
cpu: Number,
66-
},
67-
allocation: {
68-
ip: String,
69-
port: Number,
70-
},
71-
feature_limits: {
72-
databases: Number,
73-
allocations: Number,
74-
}
75-
};
76-
}
77-
78-
/**
79-
* Routes to use when building models.
80-
*
81-
* @returns {{fetch: string}}
82-
*/
83-
static routes() {
84-
return {
85-
fetch: '/api/client/servers/{identifier}',
86-
};
87-
}
88-
}
89-
90-
export class ServerCollection extends Collection {
91-
static model() {
92-
return Server;
93-
}
94-
95-
static defaults() {
96-
return {
97-
orderBy: identifier,
98-
};
99-
}
100-
101-
static routes() {
102-
return {
103-
fetch: '/api/client',
104-
};
105-
}
106-
107-
get todo() {
108-
return this.sum('done');
109-
}
110-
111-
get done() {
112-
return this.todo === 0;
113-
}
1143
}

0 commit comments

Comments
 (0)