Skip to content

Commit d39f353

Browse files
committed
Now properly supports saving the auth key in between requests
1 parent c34f418 commit d39f353

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/snapchat.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,24 @@ public function __construct($username = NULL, $password = NULL, $auth_token = NU
6868
$this->auth_token = FALSE;
6969
$this->username = FALSE;
7070

71-
if (!empty($password)) {
72-
$this->login($username, $password);
73-
}
74-
elseif (!empty($auth_token)) {
71+
if (!empty($auth_token)) {
7572
$this->auth_token = $auth_token;
7673
$this->cache = new SnapchatCache();
7774
$this->username = $username;
75+
76+
//Retrieve updates like if we performed an actual log in.
77+
$updates = $this->getUpdates(true);
78+
//Checks if login was ok.
79+
if($updates === false)
80+
{
81+
//Login with this auth token failed. Set our state to not logged in.
82+
$this->auth_token = FALSE;
83+
$this->username = FALSE;
84+
}
85+
}
86+
//Only log in if auth token wasn't set.
87+
elseif (!empty($password)) {
88+
$this->login($username, $password);
7889
}
7990
}
8091

@@ -119,6 +130,29 @@ public function login($username, $password) {
119130
return FALSE;
120131
}
121132
}
133+
/**
134+
* Gets the auth token for external storage in between requests.
135+
*
136+
* @return mixed
137+
* The auth token if succesfull, FALSE otherwise.
138+
*/
139+
public function getAuthToken()
140+
{
141+
return $this->auth_token; //Is either a string or FALSE
142+
}
143+
144+
/**
145+
* Checks if we are logged in
146+
*
147+
* @return bool
148+
* True if logged in, false otherwise
149+
*/
150+
public function isLoggedIn()
151+
{
152+
//If either one evaluates to false, inner statement is true and return becomes false.
153+
//Could probably be && but I just stole this from elsewhere in this code.
154+
return !(!$this->auth_token || !$this->username);
155+
}
122156

123157
/**
124158
* Logs out the current user.

0 commit comments

Comments
 (0)