aboutsummaryrefslogtreecommitdiffstats
path: root/app/Controllers/Http/UserController.js
blob: f78f28d6c90328f2e20202a773831644623760ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict'

const User = use('App/Models/User');
const atob = require('atob');

class UserController {

  // Register a new user
  async signup({
    request,
    response,
    auth,
    session
  }) {
    const data = request.only(['firstname', 'email', 'password']);

    try {
      const user = await User.create({
        email: data.email,
        password: data.password,
        username: data.firstname
      });
    } catch(e) {
      return response.status(401).send({
        "message": "E-Mail Address already in use",
        "status": 401
      })
    }
    
    const token = await auth.generate(user)

    return response.send({
      "message": "Successfully created account",
      "token": token.token
    });
  }

  // Login using an existing user
  async login({
    request,
    response,
    auth
  }) {
    const authHeader = atob(request.header('Authorization').replace('Basic ', '')).split(':');

    let user = (await User.query().where('email', authHeader[0]).first());
    if (!user || !user.email) {
      return response.status(401).send({
        "message": "User credentials not valid (Invalid mail)",
        "code": "invalid-credentials",
        "status": 401
      });
    }


    let token;
    try {
      token = await auth.attempt(user.email, authHeader[1])
    } catch (e) {
      return response.status(401).send({
        "message": "User credentials not valid",
        "code": "invalid-credentials",
        "status": 401
      });
    }

    return response.send({
      "message": "Successfully logged in",
      "token": token.token
    });
  }

  // Return information about the current user
  async me({
    request,
    response,
    auth,
    session
  }) {
    try {
      await auth.getUser()
    } catch (error) {
      response.send('Missing or invalid api token')
    }

    return response.send({
      accountType: "individual",
      beta: false,
      donor: {},
      email: auth.user.email,
      emailValidated: true,
      features: {},
      firstname: "Franz",
      id: "2acd2aa0-0869-4a91-adab-f700ac256dbe",
      isPremium: true,
      isSubscriptionOwner: true,
      lastname: "Franz",
      locale: "en-US"
    });
  }
}

module.exports = UserController