Updated session management.

Commit
3f46f87e82365b33d5cb95742939e381b67483c3
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
src/MyFitnessTracker/MFTSession.class.st
index 460688c0..d9b45777 100644..100644
@@ -3,78 +3,142 @@
3 3 #superclass : #WASession,
4 4 #instVars : [
5 5 'user',
6 Removed: 'dbConnection'
6 Added: 'db',
7 Added: 'mainTabs'
7 8 ],
8 9 #category : #MyFitnessTracker
9 10 }
10 11
11 12 { #category : #'as yet unclassified' }
12 Removed: MFTSession >> allProspects [
13 Added: MFTSession >> attemptLogin: aMFTUser [
14 Added: "Attempt to match aMFTUser passwordHash against an existing user password_hash in the database.
13 15
14 Removed: ^ (self session dbConnection execute: 'SELECT * FROM prospects') rows
15 Removed: collect: #email
16 Added: If a matching user is found, hydrate a MFTUser from the existingUser, and keep track of the database connection used to authenticate.
17 Added: If no matching user is found, kill the connection."
18 Added:
19 Added: | connection existingUser passwordHash |
20 Added: connection := (MFTSQLite3Database openOn:
21 Added: (Smalltalk imageDirectory / 'mft.sqlite3') fullName)
22 Added: connection.
23 Added: existingUser := ((connection
24 Added: execute: 'SELECT * FROM users WHERE username = ?'
25 Added: value: aMFTUser username) onlyRow: [
26 Added: Dictionary new ]) asDictionary.
27 Added: passwordHash := existingUser
28 Added: at: #password_hash
29 Added: ifAbsent: [ String empty ].
30 Added: connection close.
31 Added: aMFTUser passwordHash = passwordHash
32 Added: ifTrue: [ self login: (MFTUser newFromDictionary: existingUser) ]
33 Added: ifFalse: [ ^ nil ]
16 34 ]
17 35
18 36 { #category : #'as yet unclassified' }
19 Removed: MFTSession >> attemptLoginWithEmail: anEmail password: aPassword [
37 Added: MFTSession >> computeTabsFor: aMFTUser [
20 38
21 Removed: | targetUser |
22 Removed: targetUser := self findUserByEmail: anEmail.
23 Removed: targetUser ifNotNil: [
24 Removed: targetUser passwordHash = aPassword ifTrue: [ ^ self user: user ] ]
39 Added: | newTabs |
40 Added: newTabs := mainTabs.
41 Added: newTabs at: 1 put: (user isLoggedIn
42 Added: ifFalse: [ MFTPublicHomepageTab new ]
43 Added: ifTrue: [ MFTUserHomepageTab new ]).
44 Added: ^ newTabs
25 45 ]
26 46
27 47 { #category : #accessing }
28 Removed: MFTSession >> dbConnection [
48 Added: MFTSession >> db [
29 49
30 Removed: ^ dbConnection
50 Added: ^ db
31 51 ]
32 52
33 53 { #category : #accessing }
34 Removed: MFTSession >> dbConnection: anObject [
54 Added: MFTSession >> db: anObject [
35 55
36 Removed: dbConnection := anObject
56 Added: db := anObject
37 57 ]
38 58
39 Removed: { #category : #'as yet unclassified' }
40 Removed: MFTSession >> findUserByEmail: anEmail [
41 Removed:
42 Removed: ^ true
43 Removed: ]
44 Removed:
45 59 { #category : #initialization }
46 60 MFTSession >> initialize [
47 61
48 62 super initialize.
49 Removed: user := nil.
50 Removed: dbConnection := SQLite3Connection on:
51 Removed: (Smalltalk imageDirectory / 'mft.db') fullName.
52 Removed: dbConnection open
53 Removed: "TODO: Deal with database connection errors...
54 Removed: dbConnection isOpen ifFalse: [ ]"
63 Added: user := MFTUser new.
64 Added: mainTabs := OrderedCollection newFromArray: {
65 Added: MFTPublicHomepageTab new.
66 Added: MFTWorkoutsListTab placeholder.
67 Added: MFTWeightsListTab new.
68 Added: MFTLearnTab new.
69 Added: MFTProfileTab new }
55 70 ]
56 71
57 72 { #category : #login }
73 Added: MFTSession >> login: aMFTUser [
74 Added:
75 Added: | connection loginDateAndTime |
76 Added: self user: aMFTUser.
77 Added: user isLoggedIn: true.
78 Added:
79 Added: loginDateAndTime := DateAndTime now asUTC rounded.
80 Added: user lastConnectionDateTime: loginDateAndTime.
81 Added: connection := (MFTSQLite3Database openOn:
82 Added: (Smalltalk imageDirectory / 'mft.sqlite3') fullName)
83 Added: connection.
84 Added: connection
85 Added: execute: 'UPDATE users
86 Added: SET last_connection_date_time = ?
87 Added: WHERE username = ?'
88 Added: value: loginDateAndTime
89 Added: value: aMFTUser username.
90 Added: connection close.
91 Added:
92 Added: self updateUserTabs
93 Added: ]
94 Added:
95 Added: { #category : #login }
58 96 MFTSession >> logout [
59 97
98 Added: db connection close.
60 99 user := nil
61 100 ]
62 101
63 Removed: { #category : #'parent/child' }
64 Removed: MFTSession >> unregistered [
102 Added: { #category : #accessing }
103 Added: MFTSession >> mainTabs [
65 104
66 Removed: dbConnection close.
67 Removed: super unregistered
105 Added: ^ mainTabs
68 106 ]
69 107
70 108 { #category : #accessing }
109 Added: MFTSession >> mainTabs: anObject [
110 Added:
111 Added: mainTabs := anObject
112 Added: ]
113 Added:
114 Added: { #category : #'as yet unclassified' }
115 Added: MFTSession >> registerNewUser: aMFTUser [
116 Added:
117 Added: | connection |
118 Added: connection := (MFTSQLite3Database openOn:
119 Added: (Smalltalk imageDirectory / 'mft.sqlite3') fullName)
120 Added: connection.
121 Added: connection
122 Added: execute: 'INSERT INTO users(username,password_hash) VALUES (?, ?);'
123 Added: value: aMFTUser username
124 Added: value: aMFTUser passwordHash.
125 Added: connection close
126 Added: ]
127 Added:
128 Added: { #category : #'as yet unclassified' }
129 Added: MFTSession >> updateUserTabs [
130 Added:
131 Added: mainTabs := self computeTabsFor: user
132 Added: ]
133 Added:
134 Added: { #category : #accessing }
71 135 MFTSession >> user [
72 136
73 137 ^ user
74 138 ]
75 139
76 140 { #category : #accessing }
77 Removed: MFTSession >> user: anObject [
141 Added: MFTSession >> user: aMFTUser [
78 142
79 Removed: user := anObject
143 Added: user := aMFTUser
80 144 ]