 			p: password
 			verifycode: verification_code
 		@http_post login_url, form, ({text}) =>
-			user_id = @get_cookie('xunlei.com', 'userid')
-			if not user_id
+			if not @get_id_from_cookie()
 				callback? ok: false
 			else
-				@id = user_id
 				callback? ok: true
 
 	login_without_retry: (username, password, callback) ->



	commit_bt_task: ({info_hash, name, size, files}, callback) ->
		form =
			uid: @id
			btname: name
			cid: info_hash
			tsize: size
			findex: (f['id']+'_' for f in files).join('')
			size: (f['size']+'_' for f in files).join('')
			from: '0'
		jsonp = "jsonp#{current_timestamp()}"
		commit_url = "/interface/bt_task_commit?callback=#{jsonp}"
		@post commit_url, form, ({text}) ->
			if text.match "^#{jsonp}\(.*\)"
				callback ok: true, reason: 'BT task created', info_hash: info_hash
			else
				callback ok: false, reason: 'Failed be parse bt result', response: text

	add_bt_task_by_blob: (blob, callback) ->
		@upload_torrent_file_by_blob blob, (result) =>

					@password = password
					@auto_login callback


	login_check: (callback) ->
#		if @get_cookie('.xunlei.com', 'sessionid')
#			throw new Error("Not Implemented: login_check")
#		else if @get_cookie '.xunlei.com', 'lsessionid'
#			@login_enrich_cookie (result) =>
#				if result.ok
#					callback result
#				else
#					callback ok: false
#		else
#			callback ok: false, reason: 'No session found'
		if not (@get_cookie('xunlei.com', 'userid') and @get_cookie('.xunlei.com', 'sessionid'))
			@login_enrich_cookie (result) =>
				if result.ok
					callback result
				else
					if @auto_relogin
						@auto_login (result) =>
							callback result
					else
						callback ok: false, reason: 'auto-relogin is disabled'
		else
			callback ok: true

	# TODO:
	auto_relogin_for: ({url, form, pattern, callback}) ->
		if form?
			request = (callback) => @post url, form, callback
		else
			request = (callback) => @get url, callback
		if Object.prototype.toString.call(pattern) == '[object RegExp]'
			is_timeout = (text) -> pattern.match text
		else
			is_timeout = (text) -> text == pattern
		relogin = =>
			@auto_login (result) =>
				if result.ok
					request (result) =>
						callback result
				else
					callback result
		start = =>
			request (result) =>
				{text} = result
				if is_timeout(text) and @auto_relogin
					relogin()
				else
					callback result
		@login_check =>
			start()

	with_id: (callback) ->
		@login_check =>
			callback @get_id_from_cookie()

	#######
	# add #
